• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Results from a preferences page

Leolicos

Member
How do I get the results of a preferences page? I have the main application page, then when menu > preferences is clicked, a new Activity is started and goes to the preferences page. How do I, when returning to the main application page, grab the values from the preferences page? Likewise, how do I, when going TO the preferences page, set the values according to what they were previously set at?
 
When you set up the Activity derived from a PreferenceActivity, it automatically loads and saves values from the default shared preferences--you don't have to do anything.

To read the values, create a SharedPreferences object:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

If you want to programmatically set values in SharedPreferences somewhere else in your app, you create a SharedPreferences.Editor object:

SharedPreferences.Editor edit = preferences.edit();

Then set values in the object. The changes won't be written out until you call commit() on the SharedPreferences.Editor object.
 
Back
Top Bottom