片段的 childfragmentmanager getFragments() return 替换后为空
Fragment's childfragmentmanager getFragments() return null after replace
我有 MainActivity,里面有一个 FragmentLayout。当使用此代码选择选项卡(如 instagram)时,我在该布局内显示了不同的片段。
MABaseFragment fragment = mBackStackOfFragment.get(getStackNo(stack)).peek();
ft.replace(R.id.contentFrame, fragment).
currentVisibleFragment = fragment;
在其中一个片段中,我使用父片段的 childfragmentmanager 创建了 Viewpager。我用这段代码创建父片段
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
List asd = getChildFragmentManager().getFragments();
if(view == null)
{
view = inflater.inflate(R.layout.fragment_search, container, false);
setHasOptionsMenu(false);
actionBar = MAMainActivity.mainActivity.getSupportActionBar();
mViewPager = (ViewPager) view.findViewById(R.id.pager);
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.pager_title_strip);
mViewPager.setOffscreenPageLimit(numberOfTabs);
mSlidingTabLayout.setDistributeEvenly(true);
pagerAdapter = new MASearchFPA(getContext(), getChildFragmentManager());
mViewPager.setAdapter(pagerAdapter);
mSlidingTabLayout.setViewPager(mViewPager);
mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.colorPrimary);
}
@Override
public int getDividerColor(int position) {
return getResources().getColor(R.color.colorPrimary);
}
});
}
return view;
}
我保留了每个片段的引用,因为允许用户从他们离开的地方继续。它适用于其他没有嵌套片段的片段,但是当用这个嵌套片段替换当前片段时应用程序崩溃
01-11 12:36:08.239 20857-20857/tr.com.meeapps.livyapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: tr.com.meeapps.livyapp, PID: 20857
java.lang.IllegalStateException: Fragment no longer exists for key f0: index 0
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:692)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:215)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1453)
at android.view.View.dispatchRestoreInstanceState(View.java:15637)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3268)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3274)
at android.view.View.restoreHierarchyState(View.java:15615)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1135)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
因为我认为 childfragmentmanager 的 mActive 列表在调用 replace 方法后为 null,并且 return 对于 Viewpager 为 null。
所以我不知道为什么替换后子片段的mActive片段为空。有什么办法可以防止这种行为。
谢谢。
编辑
还有我的适配器class
public class MASearchFPA extends FragmentStatePagerAdapter {
Context context;
MACategoriesFragment categoriesFragment;
MAMapFragment mapFragment;
MAFavoritesFragment favoritesFragment;
FragmentManager fm;
public MASearchFPA(Context context, FragmentManager fm) {
super(fm);
this.context = context;
this.fm = fm;
categoriesFragment = new MACategoriesFragment();
mapFragment = new MAMapFragment();
favoritesFragment = new MAFavoritesFragment();
}
@Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
return categoriesFragment;
}
else if(position == 1) // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
{
return mapFragment;
}
else
{
return favoritesFragment;
}
}
@Override
public int getCount() {
return MASearchFragment.numberOfTabs;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return context.getResources().getString(R.string.categories);
case 1:
return context.getResources().getString(R.string.map);
case 2:
return context.getResources().getString(R.string.favorites);
default:
return null;
}
}
我解决了显示和隐藏方法的问题。因为 replace 方法破坏了片段的子片段的引用。通过显示和隐藏片段相对解决了我的问题。
我有 MainActivity,里面有一个 FragmentLayout。当使用此代码选择选项卡(如 instagram)时,我在该布局内显示了不同的片段。
MABaseFragment fragment = mBackStackOfFragment.get(getStackNo(stack)).peek();
ft.replace(R.id.contentFrame, fragment).
currentVisibleFragment = fragment;
在其中一个片段中,我使用父片段的 childfragmentmanager 创建了 Viewpager。我用这段代码创建父片段
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
List asd = getChildFragmentManager().getFragments();
if(view == null)
{
view = inflater.inflate(R.layout.fragment_search, container, false);
setHasOptionsMenu(false);
actionBar = MAMainActivity.mainActivity.getSupportActionBar();
mViewPager = (ViewPager) view.findViewById(R.id.pager);
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.pager_title_strip);
mViewPager.setOffscreenPageLimit(numberOfTabs);
mSlidingTabLayout.setDistributeEvenly(true);
pagerAdapter = new MASearchFPA(getContext(), getChildFragmentManager());
mViewPager.setAdapter(pagerAdapter);
mSlidingTabLayout.setViewPager(mViewPager);
mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.colorPrimary);
}
@Override
public int getDividerColor(int position) {
return getResources().getColor(R.color.colorPrimary);
}
});
}
return view;
}
我保留了每个片段的引用,因为允许用户从他们离开的地方继续。它适用于其他没有嵌套片段的片段,但是当用这个嵌套片段替换当前片段时应用程序崩溃
01-11 12:36:08.239 20857-20857/tr.com.meeapps.livyapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: tr.com.meeapps.livyapp, PID: 20857
java.lang.IllegalStateException: Fragment no longer exists for key f0: index 0
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:692)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:215)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1453)
at android.view.View.dispatchRestoreInstanceState(View.java:15637)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3268)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3274)
at android.view.View.restoreHierarchyState(View.java:15615)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1135)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
因为我认为 childfragmentmanager 的 mActive 列表在调用 replace 方法后为 null,并且 return 对于 Viewpager 为 null。
所以我不知道为什么替换后子片段的mActive片段为空。有什么办法可以防止这种行为。 谢谢。
编辑
还有我的适配器class
public class MASearchFPA extends FragmentStatePagerAdapter {
Context context;
MACategoriesFragment categoriesFragment;
MAMapFragment mapFragment;
MAFavoritesFragment favoritesFragment;
FragmentManager fm;
public MASearchFPA(Context context, FragmentManager fm) {
super(fm);
this.context = context;
this.fm = fm;
categoriesFragment = new MACategoriesFragment();
mapFragment = new MAMapFragment();
favoritesFragment = new MAFavoritesFragment();
}
@Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
return categoriesFragment;
}
else if(position == 1) // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
{
return mapFragment;
}
else
{
return favoritesFragment;
}
}
@Override
public int getCount() {
return MASearchFragment.numberOfTabs;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return context.getResources().getString(R.string.categories);
case 1:
return context.getResources().getString(R.string.map);
case 2:
return context.getResources().getString(R.string.favorites);
default:
return null;
}
}
我解决了显示和隐藏方法的问题。因为 replace 方法破坏了片段的子片段的引用。通过显示和隐藏片段相对解决了我的问题。