Why is onResume called after onRequestPermissionsResult?

I ran into some problems with android permissions. The problem is that onResume is called every time onRequestPermissionsResult is called, even if the user has already said "Never ask again."

Example:

@Override public void onResume() { super.onResume(); startLocationProvider(); } private void startLocationProvider() { if ( !locationService.requestLocationPermission( this, 0 ) ) { return; } @Override public void onRequestPermissionsResult( int requestCode, String[] permissions, int[] grantResults ) { if ( requestCode == 0 ) { if ( grantResults.length == 1 && grantResults[ 0 ] == PackageManager.PERMISSION_GRANTED ) { startLocationProvider(); } } 

It works fine until the user selects "Never ask again" and denies. I do not know why onResume is called again and again, although the dialog is not displayed to the user.

+10
android 6.0-marshmallow permissions
source share
3 answers

You need to use shouldShowRequestPermissionRationale as part of your conditional checks after checking permissions.

onResume is called so you can try the results of checking for this permission.

Here is an excerpt from what I have for the project I'm working on:

 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (grantResults.length > 0) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission Granted pickDownloadFolder(); } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) { // Permission Denied if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { result = "PERMISSION_DENIED"; Intent returnIntent = new Intent(); returnIntent.putExtra(resultKey, result); setResult(RESULT_CANCELED, returnIntent); finish(); } else { result = "PERMISSION_DENIED_FORCED"; Intent returnIntent = new Intent(); returnIntent.putExtra(resultKey, result); setResult(RESULT_CANCELED, returnIntent); finish(); } } } } 
+2
source share

Have you tried moving the permission request to onStart instead of onResume?

+1
source share

According to Simone's answer :

onResume since the permissions dialog pauses your activity.

The correct chain of events is as follows:

  1. You call requestPermissions in Activity on Create

  2. requestPermissions runs on another thread because it is designed to not block the user interface thread. So your activity goes through OnStart and then OnResume

  3. the permission request generates a dialog that launches onPause for the Activity because it is no longer in the foreground.

  4. Activity is paused at this moment, and you can see a dialog box asking whether to allow or deny permission.

  5. You make your choice, the dialog is allowed, and onResume is called in the Activity.

Also note that onPause is started by the dialog always after onStart and onResume Activity, regardless of how long it takes to execute the code in them.

And now you can also see why you shouldn't put requestPermissions in onResume .

0
source share

All Articles