Android preferences: saving in Activity or Prefere

2019-09-02 06:11发布

问题:

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 onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

Thanks a lot in advance!

回答1:

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.



回答2:

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");