1. Download our Official Android App: Forums for Android!

Apps Results from a preferences page

Discussion in 'Android Development' started by Leolicos, Jun 22, 2010.

  1. Leolicos

    Leolicos Member
    Thread Starter

    Jun 16, 2010
    Senior Software Engineer
    Chicago, IL
    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?


  2. tdieckman

    tdieckman Member

    Sep 30, 2009
    San Francisco, CA
    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.
    Leolicos likes this.

Share This Page