0

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?

Community
  • 1
  • 1
Crismar
  • 63
  • 1
  • 7

2 Answers2

1

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.

Gary Bak
  • 4,746
  • 4
  • 22
  • 39
0

Observer is a good option for your problem.

Here has a sample. OBSERVER

Hope this helps!

Maicon
  • 1
  • 1
    While this may theoretically answer the question, [it would be preferable](http://meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – ryanyuyu Mar 19 '15 at 19:01