Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No activity Exception when using viewpager and fragments inside fragments

Let me describe my project.

I use actionbarsherlock with SlidingMeno from jfeinstein10 . On my main activity i have this sliding menu with a couple of menu item that open Fragments in container. (the same as basic fragment sample in sliding menu example) In one of those fragments i have view-pager with viewpageindicator. View-pager is tied up to FragmentStatePagerAdapter with my fragments. So i have fragments inside fragments. I use getChildFragmentManager() like suggested in the doc, when using fragments inside fragments.

When i select menu item with fragment that hold view-pager for the first time everything work like it should. But when i select some other menu item and then go back to this view-pager fragment i get this exception. All other fragments don’t have this problem and in stack trace there is no reference to my code.

https://gist.github.com/4502038

Did any one encounter similar problem or have some idea what is going on.

EDIT: If i use getFragmentManager() instead of getChildFragmentManager() even thou i'm in subfragment. I don't get an exception, but one of the fragments inside viewpager doesn't get recreated after i select viewpager fragment for the second time.

I created a project on github so you guys can take a look. https://github.com/pzagor2/TestErrorApp

like image 379
pzagor2 Avatar asked Jan 10 '13 13:01

pzagor2


People also ask

Can we add activity in ViewPager?

You can't use ViewPager to swipe between Activities . You need to convert each of you five Activities into Fragments , then combine everything in one FragmentActivity with the Adapter you use with ViewPager .

How to include Fragment in activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

What is getChildFragmentManager?

getSupportFragmentManager and getChildFragmentManager FragmentManager is class provided by the framework which is used to create transactions for adding, removing or replacing fragments.

How to load Fragment in Android?

LOAD A FRAGMENT: To load a fragment in an activity at first we need to have an activity and xml file which should have a parent layout that may hold the fragment when it is loaded in that layout. For loading a fragment we need to do a fragment transaction that load the fragment in stack.


1 Answers

See this: https://code.google.com/p/android/issues/detail?id=42601 bug.

Uncommenting addToBackStack https://github.com/pzagor2/TestErrorApp/blob/master/MainActivity/src/com/example/myapp/MainActivity.java#L69 , worked for me with your project setup.

You can see the bug in the Google bug report in the first URL.

Hope this helped.

like image 79
Brosa Avatar answered Nov 05 '22 20:11

Brosa