Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 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?

like image 313
Allan Avatar asked Apr 13 '10 22:04

Allan


2 Answers

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);
like image 151
kostmo Avatar answered Nov 11 '22 19:11

kostmo


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.

like image 45
Vagnerr Avatar answered Nov 11 '22 18:11

Vagnerr