0

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

samz
  • 1,592
  • 3
  • 21
  • 37

1 Answers1

0

You have 2 options:

  1. Reload all the fragments through the main Activity, by using getActivity() in the settings fragment.

  2. Create and interface named SettingsListener that will have a function like onSettingsChanged(), and implement it in the fragments that should be updated. The settings fragment will hold a list of listeners.

Rotem
  • 1,472
  • 2
  • 11
  • 18
  • Toda, some followup questions: 1-> The fragments are managed inside a view pager, so how do i tell it to update it's fragments, or just "redraw"? – samz Dec 09 '12 at 21:36
  • 2-> classic observer/listener pattern. I forgot about the observers/listeners in it :) The problem is still the view pager. It manages the fragments so I don't know exactly how to manage the registration (and maybe removal) of listeners of the settings frag... – samz Dec 09 '12 at 21:38
  • Bevakasha :) 1&2. actually you don't have to reload all of them, you can have a function in the main activity that will update only the wanted fragments. Where to do it depends where you create the fragments - in the activity or in the ViewPager Adapter? do you recreate them in the getItem() function? or do you have a permanent instance of each fragment? – Rotem Dec 09 '12 at 21:44
  • you should make your settings fragment permanent if you don't want to lose the listeners list every time you destroy and recreate it. or you can save the listeners list in your adapter and in the getItem() function do something like this: if it is a settings fragment -> send it the listeners list, else -> add this fragment to the list – Rotem Dec 09 '12 at 22:06
  • 1
    didn't have time to implement it until now. Anyway - the only solution I found (with your help) is handling the viewpager fragments yourself. the adapter must hold a map of position->fragment - put to it on getItem, remove on destroyXX. when in need of update get hold of the view pager's adapter, iterate over the map and decide which one's need updating (I did this using an interface). – samz Dec 21 '12 at 16:49