OnRequestPermissionsResult called endlessly

I am using a runtime permission request, but there is a problem with this. The onRequestPermissionsResult callback method onRequestPermissionsResult be called endlessly. Therefore, when the user rejects the request, the application does not respond.

a permission dialog appears every time the user clicks "deny". Only by clicking "never ask again" will he not appear again. * When you click "allow" it works well - no problem.

Is there a way to cancel a method that is called once?

 if (ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) { ActivityCompat.requestPermissions( this, new String[]{ Manifest.permission.ACCESS_FINE_LOCATION }, LOCATION_PERMISSION_CUSTOM_REQUEST_CODE ); } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch( requestCode ) { case LOCATION_PERMISSION_CUSTOM_REQUEST_CODE: // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted MyManager.connect(); return; } else { // permission denied return; } default: return; } } 
+8
android permissions runtime
source share
1 answer

The problem, as you mentioned in the comments, is that the code that launches the query resolution dialog is called in the onResume method.

When the resolution of the query dialog box expires, the runtime invokes onResume in the action that called it, just like any work associated with the dialog.

In your case, the refusal of permission will again call onResume , which again displays the dialog and causes an infinite loop.

Moving this permission request to onCreate or some other thread will solve your problem.

+12
source share

All Articles