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

MultiPermissionsListener not called when reguest permissions from onPermissionGranted #262

Closed ShandyukSergey closed 4 years ago

ShandyukSergey commented 4 years ago

Expected behaviour

MultiPermissionsListener called when reguest permissions from onPermissionGranted

Actual behaviour

MultiPermissionsListener is NOT called when reguest permissions from onPermissionGranted

Steps to reproduce

  1. Request any permissions and grand them
  2. Request another permissions inside previous onPermissionGranted callback Second callback will be never notify. This behaviour was different in 2.4.0 version. Find sample code below. READ_CALENDAR listener will not be notify.
Dexter
        .withContext(context)
    .withPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    .withListener(new MultiPermissionsListener() {
        @Override
        public void onPermissionGranted() {
            Log.d("LOG", "onPermissionGranted: WRITE_EXTERNAL_STORAGE");
            Dexter
                    .withContext(context)
                    .withPermissions(Manifest.permission.READ_CALENDAR)
                    .withListener(new MultiPermissionsListener() {
                        @Override
                        public void onPermissionGranted() {
                            Log.d("LOG", "onPermissionGranted: READ_CALENDAR");
                        }

                        @Override
                        public void onPermissionDeny() {
                            Log.d("LOG", "onPermissionDeny: READ_CALENDAR");
                        }
                    })
                    .check();
        }

        @Override
        public void onPermissionDeny() {
            Log.d("LOG", "onPermissionDeny: WRITE_EXTERNAL_STORAGE");
        }
    })
    .check();

Version of the library

6.2.0

haliltprkk commented 4 years ago

I am facing the same issue, as well

haliltprkk commented 4 years ago

I have noticed it is about these two libraries that I have used for mediation over iron source advertisement

implementation 'com.ironsource.adapters:vungleadapter:4.3.1'
implementation 'com.vungle:publisher-sdk-android:6.5.3'

it is weird but when I remove these two it started to work normally 🤔

binarynoise commented 4 years ago

It also happens when you start a second dialog very quickly (~1s) after returning to the Activity that requested the permission.

I digged around a bit and found, that the previous DexterActivity gets destroyed delayed (when the UI-Thread is idle again). In the meantime the new DexterActivity has appeared and registered its callback (aka PermissionListener or MultiPermissionsListener). Then, after the new DexterActivity has shown up, the UI-/Main-Thread gets idle and the old DexterActivity gets destroyed. In that process the callback is removed even though it has been replaced by a new one.

I think, the issue would be fixed, if the lib would check, if the callback to be removed is still the callback that is expected to be there and has not been replaced.

As a consequence the DexterActivity should pass itself to onActivityDestroyed when it gets destroyed and the Dexter instance should check in its onActivityDestroyed that the current activity is the activity that called the onActivityDestroyed:

void onActivityDestroyed(Activity activityThatCalledOnActivityDestroyed) {
    if (activity == activityThatCalledOnActivityDestroyed) {
      ...
    }
}

that would also imply the null-check

pedrovgs commented 4 years ago

Thanks for the fix @binarynoise I'm releasing a new version with your fix.