2

I know that there are several similar questions.

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //...

            getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new LoginFragment()).commit();
        }

        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);

            Log.i(TAG, "DZActivity onSaveInstanceState");
        }

        @Override
        public void onRequestStarted() {

            DZActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    Log.i(TAG, "DZActivity onRequestStarted");

                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ProgressFragment()).commit();
                }
            });
        }

        @Override
        public void onSignIn(String accessToken) {

            DZActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    Log.i(TAG, "DZActivity onSignIn");

                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
                }
            });
        }

After pressing Back I start my app the second time and after pressing "Sign in" I get:

08-07 18:02:54.609: I/DZActivity(3302): DZActivity onCreate
08-07 18:02:54.659: I/DZActivity(3302): DZActivity onResume
08-07 18:03:05.349: I/DZActivity(3302): DZActivity onRequestStarted
08-07 18:03:07.929: I/DZActivity(3302): DZActivity onSignIn
08-07 18:03:19.489: I/DZActivity(3302): DZActivity onPause
08-07 18:03:19.769: I/DZActivity(3302): DZActivity onStop
08-07 18:03:19.769: I/DZActivity(3302): DZActivity onDestroy
08-07 18:03:42.799: I/DZActivity(3302): DZActivity onCreate
08-07 18:03:42.819: I/DZActivity(3302): DZActivity onResume
08-07 18:03:49.559: I/DZActivity(3302): DZActivity onRequestStarted
08-07 18:03:49.559: E/AndroidRuntime(3302): java.lang.IllegalStateException: Activity has been destroyed
08-07 18:03:49.559: E/AndroidRuntime(3302):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1376)
08-07 18:03:49.559: E/AndroidRuntime(3302):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
08-07 18:03:49.559: E/AndroidRuntime(3302):     at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)

Why do I get it if my activity was recreated and even onSaveInstanceState was not called?

pvllnspk
  • 5,667
  • 12
  • 59
  • 97
  • 1
    possible duplicate of [Getting the error "Java.lang.IllegalStateException Activity has been destroyed" when using tabs with ViewPager](http://stackoverflow.com/questions/15207305/getting-the-error-java-lang-illegalstateexception-activity-has-been-destroyed) – Ilya Gazman May 25 '15 at 12:02

1 Answers1

8

This is know issue look here

This is a bug in the nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity.

A short-term workaround is to add the following code in your fragment.

@Override
public void onDetach() {
super.onDetach();

try {
     Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
     childFragmentManager.setAccessible(true);
     childFragmentManager.set(this, null);
     } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
     } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
AnkitSomani
  • 1,152
  • 8
  • 9
  • 2
    Ok. Are you using nested fragments, if not most possibly your activity/fragment has not finished yet and you trying to relaunch it. Can you check with isFinishing () method? – AnkitSomani Aug 07 '14 at 15:38
  • @AnkitSomani Could you help us with a similar problem, just with a DialogFragment called om a menu in a MainActivity: http://stackoverflow.com/questions/34187711/activity-has-been-destroyed-when-calling-fragment-from-a-menu?noredirect=1#comment56337576_34187711 –  Dec 15 '15 at 20:07
  • for anyone who has the same problem after applying this, in my case the problem was that I was using "remove" instead of "detach". – Iman Akbari May 14 '16 at 15:16