Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Android Observer Pattern

Options
  • 12-08-2016 12:19pm
    #1
    Registered Users Posts: 6,250 ✭✭✭


    I've created an app that used to have a single fragment in the Main Activity. I've now refactored this to use tabs, so now the main activity holds three fragments though not all of them exist at the same time since android only creates the active one and the one next to it.

    If I change a setting in shared preferences, I want to notifiy all active fragments of this change and its been suggested to use the Observer pattern as per the comment on my SO question here

    Does anyone have a simple example of implementing this pattern, as I'm not totally familiar with it. I want all fragments (all instances of the same class) to subscribe to an event from the Main Activity, so when I detect a change from the settings I can notify all fragments to restart their loaders, and refresh the data in the fragment.

    Ta.


Comments

  • Registered Users Posts: 1,148 ✭✭✭punk_one82


    http://www.journaldev.com/1739/observer-design-pattern-in-java

    Pretty simple explanation and sample code there. You basically just want your subject to keep a list of it's observers. When something changes you iterate through the list of registered observers and call some update method on them.

    Not familiar with android/fragments but hopefully it's straight forward enough.


  • Registered Users Posts: 52 ✭✭TheAbstracter


    There's a library that I've used to perform decoupled communication tasks like this in Android: I can't post links as I'm a new user but search for "Greendroid EventBus". Very flexible with ordering of events and different threading options.

    An alternative is Otto from Square. You can also use the LocalBroadcastManager and define your own intents to pass messages between different components.


  • Registered Users Posts: 11,262 ✭✭✭✭jester77


    Can you not implement the OnSharedPreferenceChangeListener in each fragment to listen for the changes?

    Otherwise EventBus is OK, but I would use Rx for this. It is basically the observer pattern with a few nice extras like onCompleted and onError. You can then create an Observable to emit the changes and subscribe to it where ever you want to get notified. Really clean way of doing it.


  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    jester77 wrote: »
    Can you not implement the OnSharedPreferenceChangeListener in each fragment to listen for the changes?

    Otherwise EventBus is OK, but I would use Rx for this. It is basically the observer pattern with a few nice extras like onCompleted and onError. You can then create an Observable to emit the changes and subscribe to it where ever you want to get notified. Really clean way of doing it.

    So far, I've gone with the onPreferenceChangeListener as a solution.
    I might have a look into the Rx lib


  • Registered Users Posts: 52 ✭✭TheAbstracter


    Bear in mind you should register/unregister the pref listener in onResume()/onPause() so it won't allow you to act if those fragments are in the background.


  • Advertisement
  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    Bear in mind you should register/unregister the pref listener in onResume()/onPause() so it won't allow you to act if those fragments are in the background.

    If the fragments are in the background, as they (would?) be when the shared preferences is open, would unregistering the listener prevent it from being called thus defeating the purpose?


  • Registered Users Posts: 52 ✭✭TheAbstracter


    If the fragments are in the background, as they (would?) be when the shared preferences is open, would unregistering the listener prevent it from being called thus defeating the purpose?

    Yes, if the fragments are offscreen. That's why I suggested using eventbus, you can register/unregister in onStart/onStop and can specify that results are processed on a beckground thread


  • Registered Users Posts: 52 ✭✭TheAbstracter


    Actually if the fragment is not visible then onStop() will have been called so my suggestion is no use, don't know what I was thinking


  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    I don't see the point of un-registering the listeners, as its self defeating I think. The point of it is to listen for a preference change via user input (fragments in the background at this stage), and notifiy the fragments to reload their cursors


  • Registered Users Posts: 52 ✭✭TheAbstracter


    but you could have a resource leak if you don't unregister in onStop(), no guarantee that onDestroy will be called if the system kills your app. Why not just restart the loaders in onResume of the fragments?


  • Advertisement
  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    but you could have a resource leak if you don't unregister in onStop(), no guarantee that onDestroy will be called if the system kills your app. Why not just restart the loaders in onResume of the fragments?

    Not a bad idea. I'll try that

    Still grappling with the vastness of Android


Advertisement