ViewPager inside fragment issue

Orest picture Orest · Mar 29, 2012 · Viewed 9.9k times · Source

I'm having problem with view pager which is inside of some fragment. To work with view pager I'm using FragmentPagerAdapter. And sure I'm getting java.lang.IllegalStateException: Recursive entry to executePendingTransactions exception when trying to run my app.

Most people say it's impossible to have FragmentPagerAdapter inside other fragment, so I'm sorry if this question is invalid. I was just hoping that maybe someone has some fresh ideas about this problem and its possible solution.

P.S. I've got an advice to set adapter inside of AsyncTask (I can clarify this case if someone needs it), but this solution has some bugs related with restoring such fragment state, so it's working for me.

Answer

Otto Allmendinger picture Otto Allmendinger · Nov 14, 2012

Starting with Android 4.2, you can use nested fragments. The Android support library now also includes support for this, so you can use it with older Android versions.

The basic pattern looks like this:

Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();