13

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

1 Answer 1

25

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.

5
  • I checked my code,but can't find the setRetainInstance(true).
    – DomonLee
    Aug 27, 2014 at 7:13
  • @DomonLee : Are you calling setRetainInstance(true) in your parent Fragment?
    – Squonk
    Aug 27, 2014 at 7:33
  • I checked my code again,and find this in my parent Fragment.Then I remove that,everything is ok!! Thanks~
    – DomonLee
    Aug 27, 2014 at 9:21
  • @DomonLee : Glad to help. I did some further reading which suggested the cause was the parent Fragment. I've edited my answer.
    – Squonk
    Aug 27, 2014 at 9:30
  • It helps for solving issue occurs in Eclipse project. Thank you. By the way, the same code "setRetainInstance(true") in Android Studio runs properly.
    – Nezneika
    Oct 10, 2016 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.