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)
FragmentManager is the class responsible for performing actions on your app's fragments, such as adding, removing, or replacing them, and adding them to the back stack.
The former only executes the current transaction synchronously, whereas the latter will execute all of the transactions you have committed and are currently pending. commitNow() prevents you from accidentally executing more transactions than you actually want to execute.
At runtime, a FragmentManager can add, remove, replace, and perform other actions with fragments in response to user interaction. Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class.
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(1);
}
});
For people who use Kotlin and encounter this problem the Handler() constructor is deprecated you can use this :
Handler(Looper.getMainLooper()).post {
// interact with ViewPager here
}
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