ViewPager: Recursive entry to executePendingTransactions
Asked Answered
T

3

47

I have a ViewPager within a ViewPager and I am getting this exception

09-07 18:30:26.392: ERROR/AndroidRuntime(841): FATAL EXCEPTION: main
    java.lang.IllegalStateException: Recursive entry to executePendingTransactions
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1331)
    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:422)
    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:86)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:453)
    at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
    at android.view.View.dispatchAttachedToWindow(View.java:7974)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1857)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1862)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1862)
    at android.view.ViewGroup.addViewInner(ViewGroup.java:2968)
    at android.view.ViewGroup.addView(ViewGroup.java:2824)
    at android.support.v4.view.ViewPager.addView(ViewPager.java:537)
    at android.view.ViewGroup.addView(ViewGroup.java:2781)
    at android.view.ViewGroup.addView(ViewGroup.java:2761)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:848)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1359)
    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:422)
    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:86)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:453)
    at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
    at android.view.View.dispatchAttachedToWindow(View.java:7974)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1857)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1862)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1862)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1862)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1862)
    at android.view.ViewRoot.performTraversals(ViewRoot.java:726)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1944)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:126)
    at android.app.ActivityThread.main(ActivityThread.java:3997)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)

I am not really sure where this error comes from and I don't know too much about the FragmentManager. Do I need to flush the transactions or something like that? Or is a viewPager within a ViewPager impossible? And yes I know there are other ways to do this but I want the snappness and the scrolling for free. Also I would like to understand how it actually works.

Touch answered 7/9, 2011 at 18:36 Comment(0)
A
27

Recently I encountered the same problem and after a little investigation I was surprised to discover that you simply can't embed one fragment into another. Here you are executing one fragment transaction in another one. It's just not allowed.

New version of Support Library v4 (or Android 4.2, of course) resolves this problem. See the answer below.

Update getChildFragmentManager () added to tackle the above mentioned issue.

Aggi answered 20/9, 2011 at 7:20 Comment(2)
This is no longer true, the latest support library adds in a Child FragmentManager that enables this.Closet
you should update your answer since it is still the accepted one.Amidst
H
101

New version of Support Library v4 (or Android 4.2, ofcourse) resolve this problem much simply. For do this, simply do constructor of your custom FragmentPagerAdapter like this:

public CustomFragmentPagerAdapter(android.support.v4.app.Fragment fragment)
{
    super(fragment.getChildFragmentManager());

    // write your code here
}

This work because new Android version approve using nested Fragments

Henshaw answered 24/12, 2012 at 6:45 Comment(6)
This should be the accepted answer, the rest are out of date.Cordle
and How I will create it's instance from the Activity classTarsuss
Many upvote to this, but I have a question that how can I get the fragment in the PagerAdapter constructor?Arturo
how can use it inside an activity ?Triolet
wwat id\f we don't use support lib, just framework Fragments?Chuck
@Chuck well, childFragmentManager was added only at Android API Level 17. If your min SDK level is less than 17, there are no solution. If your min API level is < 17 then you can only use custom views instead of fragments.Henshaw
A
27

Recently I encountered the same problem and after a little investigation I was surprised to discover that you simply can't embed one fragment into another. Here you are executing one fragment transaction in another one. It's just not allowed.

New version of Support Library v4 (or Android 4.2, of course) resolves this problem. See the answer below.

Update getChildFragmentManager () added to tackle the above mentioned issue.

Aggi answered 20/9, 2011 at 7:20 Comment(2)
This is no longer true, the latest support library adds in a Child FragmentManager that enables this.Closet
you should update your answer since it is still the accepted one.Amidst
U
7

Refer to this. Display fragment viewpager within a fragment

It can be done, but has be done through an Asysnc as you have to let the first fragments transaction complete first. You can have fragments within fragments. I know because I have several projects doing it.

Upchurch answered 26/6, 2012 at 7:36 Comment(1)
This seems to work fine use a handler or AsyncTask to executePendingTransactions() this will execute it after the parent fragment has finished it's executePendingTransactions()Groundmass

© 2022 - 2024 — McMap. All rights reserved.