1

I am working with nested fragments and use the childfragmentmanager to handler the inner fragments. I get the following error when the fragments are recreated by the OS ( I test this by forcefully destroying the activity when app is backgrounded)

java.lang.IllegalStateException: Fragment no longer exists for key     android:target_state: index 4

When I add the following line to the fragment , the crash does not happen

@Override
public void onSaveInstanceState(Bundle outState){
    setTargetFragment(null, -1);
    super.onSaveInstanceState(outState);
}

I am not sure what is causing this. Has anyone faced this issue before. Following is the stack trace

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.debug/com.ui.MainActivity}: java.lang.IllegalStateException: Fragment no longer exists for key android:target_state: index 2
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.IllegalStateException: Fragment no longer exists for key android:target_state: index 2
        at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:584)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:883)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
        at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1912)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:1776)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
        at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1912)
        at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:266)
        at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
        at com.ui.MainActivity.onCreate(MainActivity.java:68)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
5
  • 1
    Can you post your stacktrace? Commented Apr 24, 2015 at 14:02
  • dumazy , updated my post with the stacktrace Commented Apr 24, 2015 at 14:39
  • Did you ever find any reason for this crash? Commented Oct 26, 2015 at 8:37
  • I found this thread helpful link Commented Mar 1, 2016 at 10:57
  • The reason is that the target fragment and the fragment itself are NOT managed by the same FragmentManager. In your case I guess, the ChildFragmentManager tries to restore a target fragment which is managed by the default FragmentManager? Commented Jan 5, 2017 at 13:56

1 Answer 1

1

Try to use getParentFragment() inside the nested fragment, instead of using setTargetFragment() and getTargetFragment()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.