Reputation: 5554
In my Android app, I use SharedPreferences
to let the user manage some settings. Now after a user changes any setting, and returning to the app from the settings page, I want my Views (Fragments) to use the latest values from SharedPreferences.
The changes could include reloading a Custom View to use a color scheme or remove filtering for a List View.
Currently only when the app is restarted, the required changes are applied. I am convinced that there is a way to solve my problem, but I am unable to figure it out.
Assume that I am supporting Android 2.2 and above, so that any newer APIs for this may not be used unless its present inside the Support Library.
Upvotes: 0
Views: 187
Reputation: 11915
You need to use the onWindowFocusChanged Method to check if the current activity has lost focus.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
// Add the code to perform on the fragments after the settings have changed.
}
Upvotes: 0
Reputation: 663
Not exactly sure what your question is but it seems to me that when your user changes a setting and then hits the back button to return to the fragment you are not seeing the changes? if this is the case it is because when the user goes back android reinstates the version of the fragment that was on the stack (the one before any changes were made). My suggestion would be to try moving the loading of the new shared prefs to the onResume method for the fragment. This way they should be loaded when the user goes back.
Try this
@Override
public void onResume(){
super.onResume();
setContentView(R.layout.currentFrag);
}
this should reload the page with the correct changes.
Upvotes: 1