ParkSangGwon / TedPermission

Easy check permission library for Android Marshmallow
1.74k stars 239 forks source link

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState #13

Closed AnirudhLoya closed 7 years ago

AnirudhLoya commented 8 years ago

05-30 16:39:29.510 8994-8994/io..E/AndroidRuntime: FATAL EXCEPTION: main Process: io.., PID: 8994 java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=10, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {io../apptitude.io.aplibrary.runtimepermissions.TedPermissionActivity}: java.lang.RuntimeException: Could not dispatch event: class apptitude.io.aplibrary.runtimepermissions.busevent.TedPermissionEvent to handler [EventHandler public void apptitude.io.aplibrary.runtimepermissions.TedInstance.onPermissionResult(apptitude.io.aplibrary.runtimepermissions.busevent.TedPermissionEvent)]: Can not perform this action after onSaveInstanceState at android.app.ActivityThread.deliverResults(ActivityThread.java:3699) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.RuntimeException: Could not dispatch event: class apptitude.io.aplibrary.runtimepermissions.busevent.TedPermissionEvent to handler [EventHandler public void apptitude.io.aplibrary.runtimepermissions.TedInstance.onPermissionResult(apptitude.io.aplibrary.runtimepermissions.busevent.TedPermissionEvent)]: Can not perform this action after onSaveInstanceState at com.squareup.otto.Bus.throwRuntimeException(Bus.java:460) at com.squareup.otto.Bus.dispatch(Bus.java:387) at com.squareup.otto.Bus.dispatchQueuedEvents(Bus.java:368) at com.squareup.otto.Bus.post(Bus.java:337) at apptitude.io.aplibrary.runtimepermissions.busevent.TedBusProvider.post(TedBusProvider.java:49) at apptitude.io.aplibrary.runtimepermissions.TedPermissionActivity.permissionGranted(TedPermissionActivity.java:100) at apptitude.io.aplibrary.runtimepermissions.TedPermissionActivity.onRequestPermissionsResult(TedPermissionActivity.java:191) at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6582) at android.app.Activity.dispatchActivityResult(Activity.java:6460) at android.app.ActivityThread.deliverResults(ActivityThread.java:3695) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)  at android.app.ActivityThread.-wrap16(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493) at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511) at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634) at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)

ParkSangGwon commented 8 years ago

@AnirudhLoya When did you use TedPermission?

hackdie commented 8 years ago

+1

Bkrickl commented 7 years ago

+1

ghost commented 7 years ago

I'm experiencing the same issue. Answering your question, I'm using TedPermission in a fragment.

What I expect: 1) User clicks on a button; 2) TedPermission shows a permission dialog; 3) in onpermissiongranted I make a fragment transaction.

In reality this happens
01-12 20:59:48.924 9264-9264/TestFragment: 20:59:48.924 - onPause 01-12 20:59:56.021 9264-9264/TestFragmentt: 20:59:56.021 - onpermissiongranted 01-12 20:59:56.119 9264-9264/TestFragment: 20:59:56.119 - onResume

The callback is called when the fragment is paused. It's somewhat logical but still sad.

Tyler-Choi commented 7 years ago

@AlexeyLitvinov I'm experiencing too. In my case,

  1. User clicks on a button in fragment;
  2. TedPermission shows a permission dialog;
  3. in onpermissiongranted (This is different from your case in this section) I make dialog fragment with show method.

I solved using FragmentTransaction: FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); TakePhotoDialog dialog = TakePhotoDialog.newInstance(...); transaction.add(dialog, ""); transaction.commitAllowingStateLoss(); //You can commit by calling commitAllowingStateLoss method regardless of onSaveInstanceState().

ParkSangGwon commented 7 years ago

@AlexeyLitvinov I recommend using TedPermission in onCreate()

ghost commented 7 years ago

@ParkSangGwon for one thing is solves nothing, for another it goes against the UX in my case. I solved this issue by switching to RxPermissions.

funct7 commented 5 years ago

This is a pretty old issue, but in case anyone is supporting Marshmallow like me: this is a weird bug that's happening on Marshmallow. I haven't confirmed if any later Android versions have the same issue, but since all the crash reports I have are all M, I'm guessing the other versions are doing alright. The P and Q are okay for sure, and so is L.

(I don't use this library but I happened to have the same issue. Just posting in case someone else lands on this page after searching on Google.)

Steps to reproduce:

Basically, the weird combination of onSaveInstanceState, permission, and supportFragmentManager seems to trigger this stupid crash.

An easy workaround is calling commitAllowingStateLoss() instead of a plain commit(). ~(An easier workaround is to tell M users to upgrade their Android OS)~ 😛

There are many posts on why you shouldn't use commitAllowingStateLoss(), but that's usually because you really shouldn't, and the exception is because you're showing a fragment at the wrong timing. This seems to be an exception because it has nothing to do with the Activity being in a fragile state. But to be safe I just call it for M only.

BTW @ParkSangGwon

I recommend using TedPermission in onCreate()

This ain't good advice. You should ask user permission only when you need it. https://developer.android.com/training/permissions/usage-notes#dont-overwhelm