开发者

After changing a preference (a setting), text showing settings doesn't update

开发者 https://www.devze.com 2022-12-27 07:12 出处:网络
I will try to explain a simple app scenario: My app goes right to a \'main view\'. In this main view I have inserted a TextView which displays current settings created by way of the PreferenceManager.

I will try to explain a simple app scenario: My app goes right to a 'main view'. In this main view I have inserted a TextView which displays current settings created by way of the PreferenceManager. For simplicity sake, let's say I have a开发者_Python百科 checkbox in my settings. When I first start my app - the TextView on my main view shows my checkbox setting correctly (true). Now I go to the settings menu, it pops-up, and then I change it to false. I go back to the main view and see no change. It still say's true even after I changed it to false. If I end the app and then re-start it - all is well and it shows my change.

Apparently the main view just stays in the background while I'm changing settings? How can I redraw or update the main view after I change settings?


You could implement OnSharedPreferenceChangeListener in your main Activity:

@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    if (PREFKEY_OF_INTEREST.equals(key))
        updateSomethingInMainView();
}

and in onCreate() call:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);


When you go into your preferences your current activity should be paused and the resumed when you go back (see the lifecycle diagram on lifecycle diagram on the android site). You should be able to trigger some kind of redraw from within onResume() I would think. That will allow the data on the page to repopulate. Some sort of invalidate() call would do it I guess.

0

精彩评论

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

关注公众号