How to get existing fragments when using FragmentPagerAdapter
Summary of the problem
Note: In this answer I'm going to reference FragmentPagerAdapter
and its source code. But the general solution should also apply to FragmentStatePagerAdapter
.
If you're reading this you probably already know that FragmentPagerAdapter
/FragmentStatePagerAdapter
is meant to create Fragments
for your ViewPager
, but upon Activity recreation (whether from a device rotation or the system killing your App to regain memory) these Fragments
won't be created again, but instead their instances retrieved from the FragmentManager
. Now say your Activity
needs to get a reference to these Fragments
to do work on them. You don't have an id
or tag
for these created Fragments
because FragmentPagerAdapter
set them internally. So the problem is how to get a reference to them without that information...
Problem with current solutions: relying on internal code
A lot of the solutions I've seen on this and similar questions rely on getting a reference to the existing Fragment
by calling FragmentManager.findFragmentByTag()
and mimicking the internally created tag: "android:switcher:" + viewId + ":" + id
. The problem with this is that you're relying on internal source code, which as we all know is not guaranteed to remain the same forever. The Android engineers at Google could easily decide to change the tag
structure which would break your code leaving you unable to find a reference to the existing Fragments
.
Alternate solution without relying on internal tag
Here's a simple example of how to get a reference to the Fragments
returned by FragmentPagerAdapter
that doesn't rely on the internal tags
set on the Fragments
. The key is to override instantiateItem()
and save references in there instead of in getItem()
.
public class SomeActivity extends Activity {
private FragmentA m1stFragment;
private FragmentB m2ndFragment;
// other code in your Activity...
private class CustomPagerAdapter extends FragmentPagerAdapter {
// other code in your custom FragmentPagerAdapter...
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// Do NOT try to save references to the Fragments in getItem(),
// because getItem() is not always called. If the Fragment
// was already created then it will be retrieved from the FragmentManger
// and not here (i.e. getItem() won't be called again).
switch (position) {
case 0:
return new FragmentA();
case 1:
return new FragmentB();
default:
// This should never happen. Always account for each position above
return null;
}
}
// Here we can finally safely save a reference to the created
// Fragment, no matter where it came from (either getItem() or
// FragmentManger). Simply save the returned Fragment from
// super.instantiateItem() into an appropriate reference depending
// on the ViewPager position.
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// save the appropriate reference depending on position
switch (position) {
case 0:
m1stFragment = (FragmentA) createdFragment;
break;
case 1:
m2ndFragment = (FragmentB) createdFragment;
break;
}
return createdFragment;
}
}
public void someMethod() {
// do work on the referenced Fragments, but first check if they
// even exist yet, otherwise you'll get an NPE.
if (m1stFragment != null) {
// m1stFragment.doWork();
}
if (m2ndFragment != null) {
// m2ndFragment.doSomeWorkToo();
}
}
}
or if you prefer to work with tags
instead of class member variables/references to the Fragments
you can also grab the tags
set by FragmentPagerAdapter
in the same manner:
NOTE: this doesn't apply to FragmentStatePagerAdapter
since it doesn't set tags
when creating its Fragments
.
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// get the tags set by FragmentPagerAdapter
switch (position) {
case 0:
String firstTag = createdFragment.getTag();
break;
case 1:
String secondTag = createdFragment.getTag();
break;
}
// ... save the tags somewhere so you can reference them later
return createdFragment;
}
Note that this method does NOT rely on mimicking the internal tag
set by FragmentPagerAdapter
and instead uses proper APIs for retrieving them. This way even if the tag
changes in future versions of the SupportLibrary
you'll still be safe.
Don't forget that depending on the design of your Activity
, the Fragments
you're trying to work on may or may not exist yet, so you have to account for that by doing null
checks before using your references.
Also, if instead you're working with FragmentStatePagerAdapter
, then you don't want to keep hard references to your Fragments
because you might have many of them and hard references would unnecessarily keep them in memory. Instead save the Fragment
references in WeakReference
variables instead of standard ones. Like this:
WeakReference<Fragment> m1stFragment = new WeakReference<Fragment>(createdFragment);
// ...and access them like so
Fragment firstFragment = m1stFragment.get();
if (firstFragment != null) {
// reference hasn't been cleared yet; do work...
}
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. - Based on explanation in the post support FragmentPagerAdapterholds reference to old fragments you should leave the creation of the fragments to the
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.
private String getFragmentTag(int viewPagerId, int fragmentPosition)
{
return "android:switcher:" + viewPagerId + ":" + fragmentPosition;
}
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#104
you don't need to override instantiateItem
nor rely on compatibility with internal makeFragmentName
method by manually creating fragment tags .instantiateItem
is a public method so you can and actually should call it in onCreate
method of your activity surrounded with calls to startUpdate
and finishUpdate
methods as described in PagerAdapter
javadoc:
A call to the PagerAdapter method startUpdate(ViewGroup) indicates that the contents of the ViewPager are about to change. One or more calls to instantiateItem(ViewGroup, int) and/or destroyItem(ViewGroup, int, Object) will follow, and the end of an update will be signaled by a call to finishUpdate(ViewGroup).
You can then by the way of the above, store references to instances of your fragments on local vars if you need. See example:
public class MyActivity extends AppCompatActivity {
Fragment0 tab0; Fragment1 tab1;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
ViewPager viewPager = (ViewPager) findViewById(R.id.myViewPager);
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
((TabLayout) findViewById(R.id.tabs)).setupWithViewPager(viewPager);
adapter.startUpdate(viewPager);
tab0 = (Fragment0) adapter.instantiateItem(viewPager, 0);
tab1 = (Fragment1) adapter.instantiateItem(viewPager, 1);
adapter.finishUpdate(viewPager);
}
class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager manager) {super(manager);}
@Override public int getCount() {return 2;}
@Override public Fragment getItem(int position) {
if (position == 0) return new Fragment0();
if (position == 1) return new Fragment1();
return null; // or throw some exception
}
@Override public CharSequence getPageTitle(int position) {
if (position == 0) return getString(R.string.tab0);
if (position == 1) return getString(R.string.tab1);
return null; // or throw some exception
}
}
}
instantiateItem
will first try to get references to existing fragment instances from FragmentManager
. Only if they don't exist yet, it will create new ones using getItem
method from your adapter and "store" them in the FragmentManager
for any future use.
It's important to note, that even if you don't need to obtain references to your fragments, you still should call instantiateItem
for all your tabs surrounded by startUpdate
/finishUpdate
in your onCreate
method like this:
adapter.startUpdate(viewPager);
// ignoring return values of the below 2 calls, just side effects matter:
adapter.instantiateItem(viewPager, 0);
adapter.instantiateItem(viewPager, 1);
adapter.finishUpdate(viewPager);
If you don't do so, then you are risking that your fragment instances will never be committed to FragmentManager
: when your activity becomes foreground instantiateItem
will be called automatically to obtain your fragments, but startUpdate
/finishUpdate
may not (depending on implementation details) and what they basically do is begin/commit a FragmentTransaction
.
This may result in references to the created fragment instances being lost very quickly (for example when you rotate your screen) and recreated much more often than necessary. Depending on how "heavy" your fragments are, it may have a non-negligible performance consequences.
Moreover, in such case instances of fragments stored on local vars may become stale: if android platform tries to obtain them from FragmentManager
for whatever reason, it will fail and thus will create and use new ones, while your vars will still be referencing the old ones.