Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using a runtime permission request, but there is a problem with that. It seems that the callback method onRequestPermissionsResult is called infinitely. So when the user denies the request the app is unresponsive..

the permission dialog reappears everytime the user clicks 'deny'. Only by clicking "never ask again" does it not reappear again. * When pressing 'allow' it works well - without any problems.

Is there any way to cancel the method being invoked after one time?

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;
                @GilMoshayof just mGoogleApiClient.connect(); . Also, It's worth mentioning that If  "allow" is clicked, then the problem doesn't occur. But when user presses 'deny' it inifintely calles onRequestPermissionsResult 
– BVtp
                Aug 15, 2016 at 11:15
                By the way, the permission dialog reappears everytime you click 'deny'. Only by click "never ask again" does it not reappear again
– BVtp
                Aug 15, 2016 at 11:21
                Could you show where your "if (ActivityCompat.checkSelfPermission...)" code is running? What method is this code inside?
– Gil Moshayof
                Aug 15, 2016 at 11:24
                Yes, of course - onResume is called when another activity finishes and returns to the current activity. The request permission dialog acts as an activity, so when it stops working, your current activity is resumed, and thus the cycle repeats :)
– Gil Moshayof
                Aug 15, 2016 at 11:28

The problem here, as you mentioned in the comments, is that the code that triggers the request permission dialog is being called in the onResume method.

When the request dialog permission finishes, the runtime calls onResume on the activity that triggered it, just as any dialog-themed activity would.

In your case, refusing the permission would trigger a call to onResume again, which would once again display the dialog and cause an endless cycle.

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

Just a point of clarification: "just as any dialog would" -- that is true for a dialog-themed activity. It is not true for a Dialog, as a Dialog does not affect the activity lifecycle. In this case, the runtime permissions dialog is a dialog-themed activity. – CommonsWare Aug 15, 2016 at 11:40

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.