Karumi / Dexter

Android library that simplifies the process of requesting permissions at runtime.
http://karumi.com
Apache License 2.0
5.23k stars 671 forks source link

Show DialogFragments from listeners causes IllegalStateException #112

Open Alamgir opened 7 years ago

Alamgir commented 7 years ago

Trying to show a dialog fragment from the successful permission listener:

Dexter.withActivity(this)
                .withPermission(Manifest.permission.CAMERA)
                .withListener(new PermissionListener() {
                    @Override public void onPermissionGranted(PermissionGrantedResponse response) {
                        dialogFragment = DialogFragment.getInstance(MyActivity.this);
                       dialogFragment.show(getSupportFragmentManager(),"dialog_frag");
                    }
                    @Override public void onPermissionDenied(PermissionDeniedResponse response) {}
                    @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission,final PermissionToken token) {}
                })
                .check();

Stacktrace Error: java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=42, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.selfycart.android.buyer/com.karumi.dexter.DexterActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

Seems to be an issue with the way Dexter brings up a new activity in the stack and dismisses the original? Can you elaborate on that lifecycle?

Alamgir commented 7 years ago

Apparently a known issue on Marshmallow: https://code.google.com/p/android/issues/detail?id=190966

pedrovgs commented 7 years ago

Any workaround we can use?

pedrovgs commented 7 years ago

We'd also need to know the library version you are using and the android SDK version where the bug is reproducible. If you could provide us a list of steps to reproduce it or a sample project it could help a lot.

Serchinastico commented 7 years ago

Hi @Alamgir

It's the first notice of this issue we have. Do you happen to know if using one of the mentioned hacks in the post effectively solves the issue? It seems like something we might want to add to the library if we can solve it by just re-posting the result in the main thread.

Alamgir commented 7 years ago

I'm using version Dexter 3.0.2 and the issue was occurring with Android 6.0.1M.

The workaround I used was a 500ms delayed post to a Handler that was instantiated on the Main thread. I don't like the workaround, but it seems to work consistently. I'm not sure if it's a good idea to add the hack to the library as this is a known bug and should be on the Android team's list of to-dos. It's your call in the end...

Alamgir commented 7 years ago

Other than this one issue - thank you so much for this library guys, saved me a ton of headache and allowed me to implement the entire permissions model in less than a day

pedrovgs commented 7 years ago

If the bug is related to the Android SDK adding a workaround to the library could not be the best option. Maybe, just updating the library documentation is enough.

Serchinastico commented 7 years ago

The only problem I see with the bug is that the Android team has marked it as WorkingAsIntended. I'll give it a try to see if just posting it again in the UI thread is good enough. Other than that, PRs are more than welcome as I might not be able to test this for some time.

pouriabagheri commented 7 years ago

@Alamgir @Serchinastico I faced the same issue I thought of using commitAllowingStateLoss() instead of commit(). Note that according to android docs it is not a good idea to use [commitAllowingStateLoss](https://developer.android.com/reference/android/app/FragmentTransaction.html#commitAllowingStateLoss()) unless you are out of options. however,I think this solution would solve the issue.

@Serchinastico any thoughts ?