开发者

Android preferences: saving in Activity or PreferenceActivity

开发者 https://www.devze.com 2023-03-27 08:00 出处:网络
I have an Activity which when clicking the menu and a button appearing there, goes to a PreferenceActivity, and then loads three ListPreferences.

I have an Activity which when clicking the menu and a button appearing there, goes to a PreferenceActivity, and then loads three ListPreferences. The ListPreference lets the user choose several values to update a remote DB, and I would like that to save those values when the application goes paused for example.

As the ListPreference are in the PreferenceActivity, how can I get those values? Where should I save the current preferences state, in the Activity or in the PreferenceActivity?

This is what I have done so far in my Activity.java:

[...]
private void updateFromPreferences() {
        Context context = getApplicationContext();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        callsFrequencyUpdate = Integer.parseInt(prefs.getString(Preferences.CALLS_FREQUENCY_PREF, "0"));
        smsFrequencyUpdate = Integer.parseInt(prefs.getString(Preferences.SMS_FREQUENCY_PREF, "0"));
        locationFrequencyUpdate = Integer.parseInt(prefs.getString(Preferences.LOCATION_FREQUENCY_PREF, "0"));
    }

    private void savePreferences() {
        SharedPreferences activityPreferences = getPreferences(Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putInt(Preferences.CALLS_FREQUENCY_PREF, callsFrequencyUpdate);
        editor.putInt(Preferences.SMS_FREQUENCY_PREF, smsFrequencyUpdate);
        editor.putInt(Preferences.LOCATION_FREQUENCY_PREF, locationFrequencyUpdate);
        editor.commit();
    }

    @Override
    protected void onPause() {
        super.onPause();
        savePreferences();
    }

And this is my Preferences.java file:

public class Preferences extends PreferenceActivity  {
    public static final String CALLS_FREQUENCY_PREF = "CALLS_FREQUENCY_PREF";
    public static final String SMS_FREQUENCY_PREF = "SMS_FREQUENCY_PREF";
    public static final String LOCATION_FREQUENCY_PREF = "LOCATION_FREQUENCY_PREF";

    SharedPreferences prefs;
    @Override
    public void onCr开发者_JAVA百科eate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

Thanks a lot in advance!


Your asking is not very clear because the answers are already in the question...

  1. Datas on PreferenceActivity are saved automatically

  2. You can access to the SharedPreferences like this

    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    String value = pref.getString("name_of_my_pref", "default_value");
    


It seems like it shouldn't matter where you write and read those values if they're within sharedPrefs. However if you're having trouble with it why not just call PreferenceActivity with startActivityForResult() in your main Activity, and within onPause() in your PreferenceActivity simply pass the information into a bundle/intent with intent.putExtras() and send it back to your main Activity with setResult(), finish()? Then do whatever you want with them in your main Activity by pulling the data with intent.getExtras()? Sorry if I missed exactly what you were asking but it's not very clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号