FragmentManager is already executing transactions. When is it safe to initialise pager after commit?
Simply use childFragmentManger()
for viewpager
inside a Fragment
mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
mPager.setAdapter(mPagerAdapter);
I had this exception when quickly replacing 2 fragments AND using executePendingTransactions(). Without calling this there was no exception.
What was my case? I open a fragment A and in its onResume() (under a condition) I ask the activity to replace the fragment with fragment B. At that point the exception occurs.
My solution was to use a Handler.post(runnable) which places the query on the end of the thread queue instead of running it immediately. This way we ensure that the new transaction will be executed after any previous transactions are completed.
So my solution was as simple as:
Handler uiHandler = new Handler();
uiHandler.post(new Runnable()
{
@Override
public void run()
{
openFragmentB(position);
}
});
If you're targeting sdk 24 and above you can use:
FragmentTransaction.commitNow()
instead of commit()
If you're targeting older versions, try calling:
FragmentManager.executePendingTransactions()
after the call to commit()