I have MainActivity and has a FragmentLayout inside. I am showing different fragments inside that layout when tabs selected (like instagram) with this code.
MABaseFragment fragment = mBackStackOfFragment.get(getStackNo(stack)).peek();
ft.replace(R.id.contentFrame, fragment).
currentVisibleFragment = fragment;
In one of that fragments i have Viewpager created with childfragmentmanager of parent fragment. I create parent fragment with this code
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;
}
And I am keeping referances of every fragment because to allow user to continue where they left of. It works well with other fragments which dont have nested fragments but when replace current fragment with this nested fragment app crashes
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$1.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)
Because I think that childfragmentmanager's mActive list is being null after replace method once called and is return null to Viewpager.
So I dont know why child fragment's mActive fragments being null after replace. Is there any way that to prevent this behaviour. Thanks.
Edit
And my adapter 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;
}
}