刷新preferences在简历preferenceActivity简历、preferences、preferenceActivity

2023-09-06 13:34:02 作者:寂寞的宣泄品

在我的应用程序,一些设置可能被改变,而 preferenceActivity 是不公开的,我快成为一个问题是,添加preferencesFromResource 被称为的onCreate ,这么说,我打开 preferenceActivity ,然后去到另一个屏幕从那里,然后做一些改变的设置,然后按返回键回到 preferenceActivity ,那么某些设置没有在布局更改。

In my app, some settings can possibly be changed while the PreferenceActivity is not open, and an issue I'm running into is that addPreferencesFromResource is called in onCreate, so say, I open the PreferenceActivity, then go to another screen from there, then do something that changes the settings, then hit the back key to go back to the PreferenceActivity, then certain settings have not changed on the layout.

所以,我怎么可以重新加载所有的 preferences 每次 onResume (或 ONSTART())被称为无重复的布局?

So, how could I re-load all the Preferences every time onResume (or onStart()) is called without duplicating the layout?

推荐答案

编辑:该解决方案将适用于API 11 +仅

edit: This solution will work for API 11 + only.

林不知道我完全理解你的问题,但你可以添加一个调用重建(),其中从我了解了该活动的活动的onResume再次通过的整个生命周期。

Im not sure I fully understand your problem, but you could add a call to recreate() into the onResume of the activity which from my understanding has the activity go through the entire lifecycle again.

为了确保你只有做到这一点时,其实也有脏数据,我会设置共享preferences,让您的活动标志知道在onResume(),它需要重新创建。

In order to make sure that you only do this when there is in fact dirty data, I would set a flag in the SharedPreferences that lets your activity know in the onResume() that it needs to be recreated.

    public void onResume(){
            super.onResume();
            SharedPreferences pref = getApplicationContext().getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
            if(pref.getBoolean("isDirtyPrefs", true))
                recreate();
        }