0

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.

Moritz
  • 10,124
  • 7
  • 51
  • 61
  • "The Fragment contains a ViewPager that contains a bunch of Fragments" -- fragments inside of fragments is not supported. http://stackoverflow.com/questions/6847460/fragments-within-fragments/6847770#6847770 – CommonsWare Sep 10 '12 at 20:58
  • @CommonsWare I would agree but it works when first created. – Moritz Sep 10 '12 at 20:59
  • 1
    Quoting Ms. Hackborn: "Trying to put a fragment within the UI of another fragment will result in undefined and likely broken behavior." I suggest that you follow Ms. Hackborn's advice, by either making the `ViewPager` use a non-fragment `PagerAdapter`, or not putting the `ViewPager` in a fragment. – CommonsWare Sep 10 '12 at 21:07
  • @CommonsWare I have read her comment before but i was hopping that would have changed by now. Seams i was wrong :( Thanks for the hint. Will try to implement my own PagerAdapter without Fragments. – Moritz Sep 10 '12 at 21:14
  • It is unlikely to change, simply because they have no way of changing the implementation in past versions of Android. It is conceivable that they could make the change and advise people to use the Android Support backport for longer (e.g., through API Level 17 or something). I'm not expecting that, though. – CommonsWare Sep 10 '12 at 21:18

2 Answers2

0

Issue to be marked as not solvable since fragments within fragments seam to be still unsupported as of Ms. Hackborn.

Moritz
  • 10,124
  • 7
  • 51
  • 61
0

Try this

 FragStartCalculation FragStartCalculation = new FragStartCalculation();
    FragmentTransaction ft = root.getSupportFragmentManager().beginTransaction();
    ft.addToBackStack(FragStartCalculation.class.getName());
    ft.replace(R.id.fragContainner, FragStartCalculation, FragStartCalculation.class.getName());
    ft.commit();
Bipin Bharti
  • 1,034
  • 2
  • 14
  • 27