12

My app keeps to report this issue on many Android platforms (4.1, 4.0.4, 2.3.6...). But I could not reproduce this issue on my phone. I have searched for this issue by Google, but the stack trace seems not the same as my.

Does someone know how the issue happening? And how to prevent it? Or how can I reproduce this error? Thank you.

Stack trace:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:278)
at android.view.View.dispatchAttachedToWindow(View.java:12064)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2707)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1339)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1131)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4611)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
at dalvik.system.NativeStart.main(Native Method)

-------------------- Edit --------------------

To Jonathan:

I have two fragments. And only one fragment overwrites the onPause callback, and the codes are as below. And I don't overwrite the other callbacks after onPause. Another callback I overwrite is the onResume callback, the codes are as below too.

Fragment:

@Override
public void onPause() {
    super.onPause();
    if (mView instanceof MyView) {
        MyView my = (MyView) mView;
        my.onPause();
    }
}

@Override
public void onResume() {
    super.onResume();
    if (mView instanceof MyView) {
        MyView my = (MyView) mView;
        my.onResume();
    }
}

MyView:

public void onPause() {
    pause = true;
}

public void onResume() {
    pause = false;
    if (mDialog != null && mDialog.isShowing()) {
        mDialog.dismiss();
        mDialog = null;
    }
}

I also trace the codes of FragmentActivity/FragmentManager, it seems if the onAttachedToWindow() be called before onPostResume(), then the issue will happen. Is it possible that the onAttachedToWindow() be called before onPostResume()?

Oleksandr
  • 6,226
  • 1
  • 46
  • 54
user2581991
  • 121
  • 1
  • 4
  • Are you doing anything in your callbacks for onPause() or anything later in the Fragment lifecycle? – Jonathan Jul 15 '13 at 02:10
  • Most likely an issue caused by screen rotation and `onSaveInstanceState`. Check `FragmentTabHost.onAttachedToWindow` line: 278. Also, if you can provide the code for the `FragmentTabHost`. – LuckyMe Jul 15 '13 at 02:24
  • 1
    @LuckyMe FragmentTabHost is part of the Android source, not something this user implemented. – Jonathan Jul 15 '13 at 05:10
  • @Jonathan I understand that, but I was thinking maybe in one of tab implementations there is something wrong. – LuckyMe Jul 15 '13 at 06:18
  • Hi all, I have updated some comments. Thanks for your help. – user2581991 Jul 16 '13 at 02:55
  • Alright, so I can't give you an answer about your specific case, but I actually just ran into this error today. Basically what was happening in my case was that I was making a request, registering a callback, and then leaving the fragment before the request was complete. This resulted in the callback being called while the reference was no longer valid, which showed a similar exception to the one you're seeing. To debug, I'd check if you have anything that could make a call after your fragment has been destroyed. – Jonathan Jul 17 '13 at 06:06
  • I have a background thread, and after the thread finish its job, it would send the message to pop out a dialog. Maybe it will cause this issue? – user2581991 Jul 18 '13 at 10:21

1 Answers1

14

This problem is caused by committing a fragment after the activity is onPaused.

A simple solution is to use FragmentTransaction.commitAllowingStateLoss() instead of FragmentTransaction.commit()

faylon
  • 7,360
  • 1
  • 30
  • 28