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

Listener not working properly on Configuration Change #267

Closed mshalomdave closed 3 years ago

mshalomdave commented 3 years ago

Expected behavior

Greetings, It seems MultiplePermissionsListener doesn't work for the current activity on configuration change(android:configChanges="orientation|screenSize|keyboardHidden") that is on orientation change

Actual behavior

It seems that on rotation the Activity gets a different identifier hence the listener won't be able to run the given method on "report.areAllPermissionsGranted()". The good thing is that the permissions are granted but the user will have to click the View again in order to run the method.

Steps to reproduce

On click of a given View, the onProfileImageClick method is called as specified below:

public interface PickerOptionListener{
void onTakeCameraSelected();
 void onChooseGallerySelected();}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
      //Other Code}

  private void onProfileImageClick() {
       Dexter.withActivity(this).withPermissions(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE).withListener(new MultiplePermissionsListener() {
        @Override
        public void onPermissionsChecked(MultiplePermissionsReport report){
            if (report.areAllPermissionsGranted()){
                showImagePickerOptions();
            }
            if (report.isAnyPermissionPermanentlyDenied()) {
                showSettingsDialog();
            }
        }
        @Override
        public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
            token.continuePermissionRequest();
        }
    }).check();
}

private void showImagePickerOptions() {
          MainActivity.showImagePickerOptions(this, new MainActivity.PickerOptionListener() {
        @Override
        public void onTakeCameraSelected() {
            launchCameraIntent();
        }
        @Override
        public void onChooseGallerySelected() {
            launchGalleryIntent();
        }
    });
}

private void showSettingsDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle(getString(R.string.dialog_permission_title));
    builder.setMessage(getString(R.string.dialog_permission_message));
    builder.setPositiveButton(getString(R.string.go_to_settings), (dialog, which) -> {
        dialog.cancel();
        openSettings();
    });
    builder.setNegativeButton(getString(android.R.string.cancel), (dialog, which) -> dialog.cancel());
    builder.show();
}
 public static void showImagePickerOptions(Context context, MainActivity.PickerOptionListener listener){AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(context.getString(R.string.lbl_set_profile_photo));
    String[] photos = {context.getString(R.string.lbl_take_camera_picture), context.getString(R.string.lbl_choose_from_gallery)};
    builder.setItems(photos, (dialog, which) -> {
        switch (which) {
            case 0:
                listener.onTakeCameraSelected();
                break;
            case 1:
                listener.onChooseGallerySelected();
                break;
        }
    });
   Dialog myDialog = builder.create();
    myDialog.show();}

Version of the library

6.0.2

pedrovgs commented 3 years ago

Hey @mshalomdave could you please try to update the library to the latest version? There are some fixes we introduced in the last releases that could fix the issue.

mshalomdave commented 3 years ago

Hello @pedrovgs. Appreciate the response. I have done as you have suggested (V. 6.2.1) but unfortunately, it's still happening. Just to add in something to what I said above. This happens when I had clicked the View and in case of no permissions granted, the Permission Dialog Appears should I rotate the screen as I am granting the permissions then,(I believe the activity gets a different identifier) and then the granting of permissions may still proceed and will work well but won't be able to call the method. Not sure if it's because it doesn't recognize the current activity after rotation.

pedrovgs commented 3 years ago

@pedrovgs could you please create a simple repository reproducing the issue? This would help us a lot with the fix.

mshalomdave commented 3 years ago

Sorry for the delay in my response. I have created a Repository(https://github.com/mshalomdave/DexterRepo). So am clarifying on the issue: This happens when I had clicked the View and in case of no permissions granted, the Permission Dialog Appears should I rotate the screen as I am granting the permissions then,(I believe the context gets a different identifier) and then the granting of permissions may still proceed and will work well but won't be able to call the method. Not sure if it's because it doesn't recognize the current context after rotation. The good thing is that the permissions are granted but the user will have to click the View again in order to run the method.

pedrovgs commented 3 years ago

@mshalomdave I'm running the code of the provided example and I don't see what's wrong. I'm afraid I don't understand where the issue is. Could you please a list of steps to reproduce the error in the sample provided and the expected behavior?

ShandyukSergey commented 3 years ago

Faced with the same issue, onPermissionsChecked doesn't call after configuration change. I was able to reproduce the issue with provided sample by @mshalomdave

STR:

  1. Click avatar icon(permissions dialog is showing)
  2. Change screen orientation to landscape
  3. Click 'Allow' button on permissions dialog
  4. Click 'Allow' button on permissions dialog once more AR: onPermissionsChecked didn't trigger ER: onPermissionsChecked have triggered
mshalomdave commented 3 years ago

Apologies for the late response. What @ShandyukSergey has said is exactly the issue

pedrovgs commented 3 years ago

I'm afraid @mshalomdave this is a Dexter known issue we face due to the activity being recreated when the screen is rotated. If you don't mind we will keep reviewing the issue in #276