0

I have a class, ExportDialog, that extends AlertDialog and also implements ActivityCompat.OnRequestPermissionsResultCallback. I'm requesting permission within the ExportDialog class by calling

ActivityCompat.requestPermissions(getOwnerActivity(),
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);

The permission is also included in the manifest as well.

However, when ActivityCompat.requestPermissions is called, onRequestPermissionsResult doesn't get called with the result of the permission request.

When testing in the emulator, I can see that the permission request is being performed and I'm able to enable or deny the permission request successfully. It's just that the code within onRequestPermissionResult never gets called.

I have checked a couple of posts like this which doesn't help my problem since my class extends AlertDialog instead.

Code snippet for requesting permission:

if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(getOwnerActivity(),
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
        return false;
    }

Code snippet for onRequestPermissionResult:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                exportCategory();
            } else {
                Toast.makeText(getContext(), "Export functionality can't be carried out.", Toast.LENGTH_LONG).show();
            }
        }
    }
}

Code snippet of class declaration (CustomDialog extends AlertDialog):

public class ExportDialog extends CustomDialog implements ActivityCompat.OnRequestPermissionsResultCallback { ... }

Thank you for your help in advance!

Community
  • 1
  • 1
Charles Li
  • 1,835
  • 3
  • 24
  • 40
  • 2
    onRequestPermissionsResult method is only executing in 'parent (activity)' where it called . – sunita Apr 13 '17 at 03:53

1 Answers1

1

you cant put that in an alert dialog. you must move it to the activity that created your dialog instead.

i dont know what exportCategory() is supposed to do but maybe you can make it public static in your dialog or move it to another separate static class and then call it from your activity.

of if that function is dependant to your object of alert dialog then you need to hold an reference to your alert dialog in your activity and then call that function (exportCategory()) with that alert dialog instance.

Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52