Reputation: 63
I have an app that displays distances and volumes. I have a SettingsFragment that allows me to choose the units for distance, mile or km, and for volume, gallon or liter. Changing between units works but not right away, I have to flip the phone or reset the app to see the changes. I want the setting change to be reflected right away on the main activity but I'm not sure how to accomplish this. I have done some reseach and found a possible solution: The settings guide suggest that I should implement the OnSharedPreferenceChangeListener interface to listen for changes. But this is implemented on PreferenceFragment and not on a different activity. This is a similar solution.
I'm confused on what is the proper way to listen for the setting change on the main activity and how, when the change happens, I should refresh the view. Can anyone please clarify?
Upvotes: 0
Views: 429
Reputation: 1
Observer is a good option for your problem.
Here has a sample. OBSERVER
Hope this helps!
Upvotes: 0
Reputation: 4798
The answer in the second link, using the onSharedPreferenceChangeListener() is what you need. Setup the listener in the fragment and then notify the main activity with a call in your fragment:
((MainActivity)getActivity()).updateData(updatedData);
You will have to implement the public method updateData(...) in your main activity.
Upvotes: 1