I have problem making my fragments communicating with each other through the Activity
, which is using the FragmentPagerAdapter
, as a helper class that implements the management of tabs and all details of connecting a ViewPager
with associated TabHost
. I have implemented FragmentPagerAdapter
just as same as it is provided by the Android sample project Support4Demos.
The main question is how can I get particular fragment from FragmentManager
when I don't have neither Id or Tag? FragmentPagerAdapter
is creating the fragments and auto generating the Id and Tags.
I always use this base class, when I need to access child fragments or primary (currently visible) fragment. It doesn't rely on any implementation details and it takes care for lifecycle changes, because overwritten methods get called in both cases - when a new fragment instance created and when instance gets received from FragmentManager.
Just go on try this code,
I have found answer on my question based on following post: reusing fragments in a fragmentpageradapter
Few things I have learned:
getItem(int position)
in theFragmentPagerAdapter
is rather misleading name of what this method actually does. It creates new fragments, not returning existing ones. In so meaning, the method should be renamed to something likecreateItem(int position)
in the Android SDK. So this method does not help us getting fragments.FragmentPagerAdapter
and in so meaning you have no reference to the Fragments or their tags. If you have fragment tag though, you can easily retrieve reference to it from theFragmentManager
by callingfindFragmentByTag()
. We need a way to find out tag of a fragment at given page position.Solution
Add following helper method in your class to retrieve fragment tag and send it to the
findFragmentByTag()
method.NOTE! This is identical method that
FragmentPagerAdapter
use when creating new fragments. See this link http://code.google.com/p/openintents/source/browse/trunk/compatibility/AndroidSupportV2/src/android/support/v2/app/FragmentPagerAdapter.java#104The way I did it is define an Hashtable of WeakReferences as follows:
Then I wrote the getItem() method like this:
Then you can write a method:
This seems to work well and I find it a little less hacky than the
trick, as it does not rely on how the FragmentPagerAdapter is implemented. Of course if the fragment has been released by the FragmentPagerAdapter or if it has not been yet created, getFragment will return null.
If anybody finds something wrong with this approach, comments are more than welcome.
The main road block with getting a handle to the fragments is you can not rely on getItem(). After an orientation change, references to the fragments will be null and getItem() is not called again.
Here's an approach that does not rely upon the implementation of FragmentPagerAdapter to get the tag. Override instantiateItem() which will return the fragment created from getItem() or found from the fragment manager.
See this post on returning fragments from the FragmentPagerAdapter. Does rely on you knowing the index of your fragment - but this would be set in getItem() (at instantiation only)