Fragments in ViewPager
are cached.
The correct way that makes use of the newer APIs are LocalBroadcastManager
or alternatively GreenDroid , Otto or any other event bus libraries.
Now, there will be problems unless your fragment count == getOffscreenPageLimit()
, because you will certainly have to unregister your broadcast receiver/ event at onPause()
, but doing that will make your fragment not react to broadcasts. In Otto and GreenDroid, there are sticky events that will fire and "stick", so that later when your fragment resume, it can look for sticky events, consume(otherwise they will stay there) them, and update it's content accordingly.
There are also many pros of using Eventbus libraries:
- Decouple UI components
- get rid of listeners everywhere
- your activity will no longer have to be a proxy, fragments can talk to each other directly