I'm dealing with on demand permissions in Android and I found a confusing situation. My objective is to display a dialog with a "go to app settings" button if the user has selected "Never ask again" in a permission.
OK, if the user selects "Never ask again" then, next time you request the permission you can know it doing this in the method onRequestPermissionsResult
public boolean checkIfShouldShowGoToSettingsDialog(String permissions[], int[] grantResults){
for (int i=0; i<permissions.length; i++){
if (grantResults[i] == PackageManager.PERMISSION_DENIED && !ActivityCompat.shouldShowRequestPermissionRationale(SectionManager.getInstance().getCurrentActivity(), permissions[i])) {
return true;
}
}
return false;
}
The problem is that if you do that, each time you call requestPermission()
, then, onRequestPermissionsResult
is being called again even with the permission denied previously and even with the checkbox selected. So checkIfShouldShowGoToSettingsDialog
is returning true
because the permission is DENIED
and shouldShowRequestPermissionRationale is returning false
. I don't want that. I want to display the "go to app settings" dialog only the first time the user has selected "Don't ask again".
So I tried something:
I added this validation before calling requestPermission()
. I tried removing the permissions which returns false to the method shouldShowRequestPermissionRationale()
because it's supposed that those permissions has the checkbox selected:
public String[] removePermanentlyDeniedPermissionsFromArray(String[] permissions){
List<String> result = new ArrayList<>();
for (String permission : permissions) {
if (ActivityCompat.shouldShowRequestPermissionRationale(SectionManager.getInstance().getCurrentActivity(), permission)) {
result.add(permission);
}
}
return result.toArray(new String[result.size()]);
}
It worked perfectly but... the problem is that the first time you open the app, the first time you are going to request the permissions... shouldShowRequestPermissionRationale()
returns FALSE for all of them! so no permissions are being requested.
Then, how can I avoid a callrequestPermission()
if the user has checked "Never ask again" ?