avast / android-styled-dialogs

Backport of Material dialogs with easy-to-use API based on DialogFragment
Apache License 2.0
2.15k stars 450 forks source link

Exception in onResume #103

Closed sohayb closed 9 years ago

sohayb commented 9 years ago

Hello, I'm receiving an exception when trying to show the dialog in the onResume function, here's the code:

SimpleDialogFragment.createBuilder(this, getSupportFragmentManager())
                    .setMessage("Location is disabled")
                    .setPositiveButtonText(android.R.string.yes)
                    .setNegativeButtonText(android.R.string.no)
                    .setRequestCode(REQUEST_LOCATION_OFF)
                    .show();

and here's the exception stack:

java.lang.RuntimeException: Unable to resume activity {com.my.app/com.my.app.ui.activities.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2958)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2987)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:157)
       at android.app.ActivityThread.main(ActivityThread.java:5377)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
       at android.support.v4.app.FragmentManagerImpl.checkStateLoss(SourceFile:1365)
       at android.support.v4.app.FragmentManagerImpl.enqueueAction(SourceFile:1383)
       at android.support.v4.app.BackStackRecord.commitInternal(SourceFile:636)
       at android.support.v4.app.BackStackRecord.commit(SourceFile:615)
       at android.support.v4.app.DialogFragment.show(SourceFile:138)
       at com.avast.android.dialogs.core.BaseDialogBuilder.show(SourceFile:89)
       at com.my.app.ui.activities.MainActivity.onResume(SourceFile:974)
       at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1210)
       at android.app.Activity.performResume(Activity.java:5507)
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2948)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2987)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:157)
       at android.app.ActivityThread.main(ActivityThread.java:5377)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
       at dalvik.system.NativeStart.main(NativeStart.java)

Do you have any idea of what might be the problem?

davidvavra commented 9 years ago

Hi, you should not show a dialog after activity/fragment has saved it's state. Usually it helps to add isAdded() check in the fragment. Or you can call showAllowingStateLoss, but it's highly discouraged.

sibelius commented 8 years ago

@davidvavra could you please provide a code snippet to fix this?