2

I have several retained fragments in my activity (up to 3) that are in charge of making different small task using asynkTask (from now smallRetainedFragments). The documentation says is a good practice to centralize the communication between fragments in the activity, so the fragments do not need to know if there is another fragment or the activity that finally will response to they requirements, so using them from other fragments (normal => with views) centralizing the communication in the activity, for example pressing a button, works great.

Now I need to make another retain fragment, one doing a long-time big task (from now newRetainedFragment), that needs to interact with the smallRetainedFragments.

I had tried the communication centralized in the activity, and if I don't rotate the screen it works great, but if I rotate the screen, sometimes the newRetainedFragment reference to the activity is invalid (during rotation because of recreation of the activity) and it can't communicate to the smallRetainedFragments to make a petition of work.

So I'm clear how to communicate fragments, but I have some troubles making communication between retained fragments.

Thanks in advance.

  • To comunicate fragments with other fragments you must have to use Listeners. Look this documentation about how it works http://developer.android.com/intl/es/training/basics/fragments/communicating.html and in the case of rotate the screen you need to use onSaveInstanceState and then onRestoreInstanceState, for those options check this documentation http://developer.android.com/intl/es/training/basics/activity-lifecycle/recreating.html – LordCommanDev Nov 27 '15 at 14:26
  • Your links work to understand the recommended way to communicate 2 (or more) fragments using listeners. But in my case my fragments are retained fragments, so they are "independent" of the activity lyfecicle, so during rotation, the activity (before rotate) is destroying or destroyed and after rotation a new activity is created. The onAttach method on the fragment update the listener, but meanwhile can be a time where there is no valid listener and the retained fragment could need it because is processing data in background and need the collaboration of the other retained fragment. – Niesteszeck Nov 27 '15 at 18:55

0 Answers0