Timothy Frisch
Timothy Frisch

Reputation: 2809

IllegalStateException Activity has been destroyed - Triple-nested Fragments

I have a Fragment, let's call it... F1 containing a Fragment F2; which in turn contains a Fragment F3.

When I launch my application I receive this error:

IllegalStateException: Activity has been destroyed

which refers to this line of code:

        getChildFragmentManager().beginTransaction().add(R.id.fragmentcontainer, fragment_profile_list.newInstance()).commit();

Where fragmentcontainer is a FrameLayout within my primary upper level Fragment F1. Where F2 is placed within F1's fragmentcontainer.

Any ideas on how to go about solving this; I tried doing a search and the other results said to hardcode a Fragment field and allocate it immediately but that did not resolve the matter either.

Upvotes: 1

Views: 546

Answers (1)

danz
danz

Reputation: 11

You can dump the internal state of F2 before adding F3. If you see mActivity != null it will be fine. Otherwise you will get the exception.

dump("", null,  new PrintWriter(System.out, true), null);

I am not sure what made mActivity = null in your case. In my case, I called the fragment's getChildFragmentManger too early before the fragment was attached (when mActivity = null). When it is called for the first time, getChildFragmentManager internally initializes mChildFragmentManager for the fragment. As result, I ended up with a mChildFragmentManager with a null mActivity. Later when I tried to use it to add a child fragment, I got the 'Activity has been destroyed' exception.

Upvotes: 1

Related Questions