I successfully converted my app from activities to a tabbed/fragmented app with 2 tabs/fragments and swiping between them 🙂
Right now I have 2 tabs:
1 – The main tab – shows textfields and stuff according to settings stored in shared preferences.
2 – The settings – offers some inputs to change these shared preferences.
The problem is that when I change the settings (shared prefs) the main tab doesn’t get updated according to the new shared preferences.
I’m looking for a solution to update the main tab, and possibly another future tab or two, when the shared preferences are changed (from the settings tab) with as much decoupling as possible.
I don’t want the settings tab to explicitly hold refs to the other fragments and perform the updates…
I tried implementing some kind of observer pattern where the settings tab announces that update is needed after a settings change but did not find how to get the other tabs to actually perform the change because they are in a view pager that kinda complicates the matter…
Any help is appreciated.
I’m using the TabsAdapter from:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
You have 2 options:
Reload all the fragments through the main Activity, by using
getActivity()in the settings fragment.Create and interface named
SettingsListenerthat will have a function likeonSettingsChanged(), and implement it in the fragments that should be updated. The settings fragment will hold a list of listeners.