Question

I have 3 Fragments A, B & C.

A contains a grid of items, B fragment has a form to add data. All data moves to/from the server.

C fragment is a preference Fragment.

When the activity starts A fragment's onCreateView method is called where I fetch the latest data from server and inflate into my grid.

When I swipe to B fragment and add some new data. After I swipe back to A fragment grid doesn't get updated. I tried to call my update method in onResume method of Fragment A. But no help.

But When I swipe to C fragment and swipe back to B fragment the list gets updated.

Was it helpful?

Solution

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:

  1. Decouple UI components
  2. get rid of listeners everywhere
  3. your activity will no longer have to be a proxy, fragments can talk to each other directly

OTHER TIPS

Communication between Fragments is, per documentation, done with Interfaces.

Check an example in this answer or in the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top