Results from a preferences page

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

  1. Leolicos

    Leolicos Well-Known Member
    15

    Jun 16, 2010
    54
    0
    15
    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?
     

    Advertisement

  2. tdieckman

    tdieckman Well-Known Member
    16

    Sep 30, 2009
    49
    5
    16
    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

Loading...