The previous example describes how to use Preferenceactivity to display modify application preferences, and the user's modifications to preferences are automatically stored in the corresponding Shared preferences of the application.
This example describes how to obtain a preference value by preferenceactivity from an activity. For example, in practical applications through the Preferenceactivity interface to obtain user preferences or configuration.
Because you want to return a value from preferenceactivity, you use Startactivityforresult to start the preferenceactivity derived class.
Advancedpreferences is a preferenceactivity derived class that defines two preferences entries for r.xml.advanced_preferences. One is the custom preference (described later in mypreference), whose value type is an integer and the other is Checkboxpreference, whose UI is as follows:
The purpose of this example is to obtain the Mypreference value through the Advancedpreferences setting, mypreference in the r.xml.advanced_preferences definition as follows:
<com.example.android.apis.app.mypreference
android:key= "My_preference"
android:title= "@string/title_my_preference"
android:summary= "@string/summary_my_preference"
Android:defaultvalue= "100″/>
We can see that its key is defined as my_preference.
Invoke the Advancedpreferences code as follows:
Intent launchpreferencesintent
= new Intent (). SetClass (this, advancedpreferences.class);
Make it a subactivity so we know when it returns
Startactivityforresult (Launchpreferencesintent,
Request_code _preferences);