问题总结
注意:在这个答案中,我将参考FragmentPagerAdapter及其源代码。但是一般的解决方案也应该适用于FragmentStatePagerAdapter。
如果您正在阅读此书,您可能已经知道FragmentPagerAdapter/ FragmentStatePagerAdapter是Fragments为您创建的ViewPager,但是在Activity重新创建(无论是设备旋转还是系统杀死您的App以重新获得内存)之后,这些Fragments都不会再次创建,而是创建它们从中检索到的实例FragmentManager。现在说您Activity需要参考这些内容Fragments以进行处理。您不具有id或tag为这些创建Fragments,因为FragmentPagerAdapter 它们设置在内部。所以问题是如何在没有这些信息的情况下获得对它们的引用...
当前解决方案的问题:依靠内部代码
我在此问题和类似问题上看到的许多解决方案都依赖于Fragment通过调用FragmentManager.findFragmentByTag()和模仿内部创建的标记"android:switcher:" + viewId + ":" + id来获得对现有标记的引用:。这样做的问题是您依赖内部源代码,众所周知,内部源代码不能保证永远保持不变。Google的Android工程师可以轻松地决定更改tag结构,这将破坏您的代码,使您无法找到现有的引用Fragments。
无需内部解决方案的替代解决方案 tag
这是一个简单的示例,该示例说明如何获取对的Fragments返回引用FragmentPagerAdapter,而不依赖于上的内部tags集合Fragments。关键是要覆盖instantiateItem()和保存的引用在那里,而不是在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();
}
}
}
或者,如果您更喜欢使用tags而不是类的成员变量/引用,则Fragments也可以tags通过FragmentPagerAdapter相同的方式获取该集合:注意:这不适用于FragmentStatePagerAdapter该集合,因为tags在创建它时未设置它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;
}
请注意,此方法不依赖于模仿内部tag集合FragmentPagerAdapter,而是使用适当的API来检索它们。这样,即使您tag将来的版本发生更改,SupportLibrary您仍然可以安全使用。
不要忘记,根据您的设计Activity,Fragments您正在尝试进行的工作可能存在或可能不存在,因此您必须null在使用引用之前进行检查以解决这一问题。
另外,如果您正在使用FragmentStatePagerAdapter,则您不想保留对您的硬引用,Fragments因为您可能有很多硬引用,而硬引用将不必要地将它们保留在内存中。而是将Fragment引用保存在WeakReference变量中,而不是标准引用中。像这样:
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...
}