FileDocCategorySizeDatePackage
LaunchingPreferences.javaAPI DocAndroid 1.5 API4054Wed May 06 22:41:08 BST 2009com.example.android.apis.app

LaunchingPreferences

public class LaunchingPreferences extends android.app.Activity implements android.view.View.OnClickListener
Demonstrates launching a PreferenceActivity and grabbing a value it saved.

Fields Summary
private static final int
REQUEST_CODE_PREFERENCES
private android.widget.TextView
mCounterText
Constructors Summary
Methods Summary
protected voidonActivityResult(int requestCode, int resultCode, android.content.Intent data)

        super.onActivityResult(requestCode, resultCode, data);
        
        // The preferences returned if the request code is what we had given
        // earlier in startSubActivity
        if (requestCode == REQUEST_CODE_PREFERENCES) {
            // Read a sample value they have set
            updateCounterText();
        }
    
public voidonClick(android.view.View v)

        
        // When the button is clicked, launch an activity through this intent
        Intent launchPreferencesIntent = new Intent().setClass(this, AdvancedPreferences.class);
        
        // Make it a subactivity so we know when it returns
        startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES);
    
protected voidonCreate(android.os.Bundle savedInstanceState)

    
    
        
        super.onCreate(savedInstanceState);

        /*
         * If this were my app's main activity, I would load the default values
         * so they're set even if the user does not go into the preferences
         * screen. Another good place to call this method would be from a
         * subclass of Application, so your default values would be loaded
         * regardless of entry into your application (for example, a service or
         * activity).
         */  
        PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);
        
        // Simple layout
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);
        
        // Create a simple button that will launch the preferences
        Button launchPreferences = new Button(this);
        launchPreferences.setText(getString(R.string.launch_preference_activity));
        launchPreferences.setOnClickListener(this);
        layout.addView(launchPreferences, new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        
        mCounterText = new TextView(this);
        layout.addView(mCounterText, new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        
        updateCounterText();
    
private voidupdateCounterText()

        // Since we're in the same package, we can use this context to get
        // the default shared preferences
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        final int counter = sharedPref.getInt(AdvancedPreferences.KEY_MY_PREFERENCE, 0);
        mCounterText.setText(getString(R.string.counter_value_is) + " " + counter);