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!