I have an activity hosting two fragments. The activity starts off showing a loader while it loads an object. The loaded object is then passed to both fragments as arguments via newInstance methods and those fragments are attached.
final FragmentTransaction trans = getSupportFragmentManager().beginTransaction(); trans.replace(R.id.container1, Fragment1.newInstance(loadedObject)); trans.replace(R.id.container2, Fragment2.newInstance(loadedObject)); trans.commit();
The second fragment contains a android.support.v4.view.ViewPager and tabs. onResume we initialise it like follows
viewPager.setAdapter(adapter); viewPager.setOffscreenPageLimit(adapter.getCount()); //the count is always < 4 tabLayout.setupWithViewPager(viewPager);
The problem is android then throws
java.lang.IllegalStateException: FragmentManager is already executing transactions
With this stack trace: (I took android.support
out of the package names just for brevity)
v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1620) at v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637) at v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143) at v4.view.ViewPager.populate(ViewPager.java:1235) at v4.view.ViewPager.populate(ViewPager.java:1083) at v4.view.ViewPager.setOffscreenPageLimit(ViewPager.java:847)
The data shows if setOffscreenPageLimit(...);
is removed. Is there another way to avoid this issue?
When in the lifecycle is the fragment transaction complete so that I can wait to setup my pager?
Simply use childFragmentManger()
for viewpager
inside a Fragment
mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager()); mPager.setAdapter(mPagerAdapter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With