安卓preferences:保存在活动或preferenceActivity存在、preferences、preferenceActivity

2023-09-12 06:05:24 作者:本性难移

我有一个活动其中点击菜单,出现有一个按钮时,都到了 preferenceActivity ,然后载入了三个目录preferences 。 该目录preference 用户可从中选择几个值来更新远程数据库,我想,要挽救这些数据,并使应用程序进入暂停的例子。

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.

由于目录preference preferenceActivity ,我怎么能得到这些值? 我应该在哪里保存当前preferences状态,在活动 preferenceActivity

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?

这是我在迄今所做的我 Activity.java

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();
    }

这是我的 preferences.java 文件:

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!

推荐答案

现在看来似乎并不重要,你写和读这些值,如果他们共享preFS内。但是,如果您在使用它的麻烦为什么不直接调用preferenceActivity与startActivityForResult()在你的主要活动,并在在onPause()在preferenceActivity简单地将信息传递到一个包/意图与intent.putExtras (),并将其与的setResult(),完成(送回到你的主要活动)?然后做任何你在你的主要活动要与他们用intent.getExtras拉低数据()?很抱歉,如果我错过了什么你问,但它不是很清楚。

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.