I have an Activity that contains a Fragment container (FrameLayout) in which i place a Fragment during Activity.onCreate(). The Fragment contains a ViewPager that contains a bunch of Fragments. All of this works fine. The pages content displays and i can nicely swipe through the pages, even seeing actions in the action bar chance as the contributing pages become visible.
Now i replace the Fragment containing the ViewPager with another Fragment:
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content, new NewFragment(), tag);
transaction.addToBackStack(null);
transaction.commit();
Replacing works fine. The new Fragment is displayed. The problem comes up when i hit the back button which places the Fragment with the ViewPager back into the container view. When using a regular FragmentPagerAdapter i get the following Exception:
FATAL EXCEPTION: main
java.lang.IllegalStateException: Recursive entry to executePendingTransactions
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1403)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:895)
at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:380)
at com.domain.app.MyFragment.onViewCreated(MyFragment.java:39)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:895)
at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:380)
at com.domain.app.MyFragment.onViewCreated(MyFragment.java:39)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:712)
at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1480)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:447)
at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:166)
at android.app.Activity.onKeyUp(Activity.java:2099)
As can be seen MyFragment.onViewCreated() is executed twice, where in the second pass an exception is thrown. I have attempted to step through the ViewPager and FragmentManager code but that is a tough business.
In addition i have replaced the FragmentPagerAdapter with a FragmentStatePagerAdapter which actually manages to recreate the fragment containing the ViewPager but not the ViewPager itself. The content of the pages is not visible although some pages apear to be on the Pager. Swiping is unpredictable in regards to the onPageSelected callback and sometimes actions in the action bar can be seen that are contributed from a fragment IN the ViewPager. But all of that is pretty sporadic.
Any help would be greatly appreciated. Thanks.