Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Can't retain fragments that are nested in other fragments

there is sth wrong with my Application,I try to add three Fragments in another Fragment,then the Eclipse show this:

java.lang.IllegalStateException: Can't retain fragements that are nested in other fragments

this is my code in the childFragments

getChildFragmentManager().beginTransaction()
            .add(R.id.shop_fragment_container, shopTopOneFragment)
            .add(R.id.shop_fragment_container, shopTopTwoFragment)
            .add(R.id.shop_fragment_container, shopTopThreeFragment)
            .hide(shopTopTwoFragment).hide(shopTopThreeFragment)
            .show(shopTopOneFragment).commit();

any help will be appreciated

like image 472
DomonLee Avatar asked Aug 27 '14 06:08

DomonLee


People also ask

How to transfer data between two fragments in Android?

To pass data between fragments in the same fragment manager, the listener should be added to the destination fragment with requestKey in order to receive the result produces from another fragment with the same key.

Can fragments be used in multiple activities?

You can use multiple instances of the same fragment class within the same activity, in multiple activities, or even as a child of another fragment. With this in mind, you should only provide a fragment with the logic necessary to manage its own UI.

How can I tell if an Android fragment is destroyed?

Since all fragments are destroyed if the activity is destroyed, a simple answer could be calling getActivity(). isDestroyed() returning true if the activity is destroyed, therefore the fragment is destroyed.


1 Answers

Can't retain fragements that are nested in other fragments

This is a limitation of nested Fragments. I'm guessing one or more of your child Fragments have setRetainInstance(true) somewhere in their code. You need to remove that to prevent the error.

EDIT: On further reading it seems if the parent Fragment is calling setRetainInstance(true) then it will cause the same exception due to the fact that attempting to retain the parent instance also attempts to retain the child Fragments.

like image 99
Squonk Avatar answered Oct 02 '22 12:10

Squonk