FragmentManager is already executing transactions

breakline picture breakline · Jan 16, 2017 · Viewed 8k times · Source

So I have this problem when using the ViewPager with a FragmentStatePagerAdapter AND then I'm trying to select the current page (i.e. show the second page to the user instead of the first one) that I get this exception from time to time.

The issue seems to be concurrency, as the viewpager is still adding/initializing the fragments i.e. currently running a fragment transaction while I'm trying to set the current page. Something like this:

pagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager());
viewPager.setAdapter(pagerAdapter);
pagerAdapter.addItems(...) //Addings ID's here for each fragment, this is where the adapter notifies the viewpager
viewPager.setCurrentItem(1); //This is where the two transactions at the same time might happen

The question is: how should I know when the viewpager finished initializing the fragments so it's safe to set the current page?

The exact exception is this:

Fatal Exception: java.lang.IllegalStateException: FragmentManager is already executing transactions
   at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1631)
   at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
   at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
   at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
   at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
   at android.view.View.measure(View.java:18916)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
   at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703)
   at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
   at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1367)
   at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:768)
   at android.view.View.measure(View.java:18916)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
   at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
   at android.view.View.measure(View.java:18916)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
   at android.view.View.measure(View.java:18916)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
   at android.view.View.measure(View.java:18916)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
   at android.view.View.measure(View.java:18916)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5972)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
   at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2693)
   at android.view.View.measure(View.java:18916)
   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2231)
   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1307)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1549)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1198)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6268)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:873)
   at android.view.Choreographer.doCallbacks(Choreographer.java:676)
   at android.view.Choreographer.doFrame(Choreographer.java:606)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:859)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:168)
   at android.app.ActivityThread.main(ActivityThread.java:5885)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

Answer

            Handler handler = new Handler();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    viewPager.setCurrentItem(1);
                }
            });