13

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?

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
Allan
  • 1,023
  • 4
  • 13
  • 20

2 Answers2

23

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);
kostmo
  • 6,222
  • 4
  • 40
  • 51
4

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.

Vagnerr
  • 2,977
  • 3
  • 33
  • 46
  • Thanks for your help, I will begin to look into this now - I appreciate your time! – Allan Apr 13 '10 at 23:16
  • I'm a beginner so I hope this isn't too difficult for me - (I'll take more help and examples from others too) – Allan Apr 13 '10 at 23:25