14

The shouldShowRequestPermissionRationale method returns false the first time.

I have the following code in a Fragment:

if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
    requestPermissions(new String[]{READ_CONTACTS}, 0);
} else {
    Toast.makeText(getActivity(), "FALSE", Toast.LENGTH_SHORT).show();
    snackBarInfo.dismiss();
}

Has anyone else encountered this?

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
Terry Grosso
  • 211
  • 2
  • 9

1 Answers1

1

Yes, this is by design. The idea is that if the permission is not granted and the shouldShowRequestPermissionRationale method returns false, then the app should request the permission from the OS.

When the user denies the permission request, then the shouldShowRequestPermissionRationale method will return true. At this point, you should show some custom UI to the user explaining why the permission is required.

See the Workflow for requesting permissions section in the Request app permissions page for further details.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
  • 1
    Thanks for explaining it. But it feels like a strange design decision to request permission immediately and if they deny show explanation view. Also the example screenshots in this page https://developer.android.com/develop/ui/views/notifications/notification-permission#wait-to-show-prompt give wrong impression of `shouldShowRequestPermissionRationale()` returning true before asking permissions for the first time. Would be good if that article mentioned that we have a chance to ask for permissions 2 times. – Marat Sep 05 '22 at 08:27
  • The Android Developers documentation doesn't prescribe when to show the explanation view from what I remember. You can show this explanation view immediately after the permission request is denied or you can show it the next time it makes sense in your app to request the permission. That said, I do think the permission request APIs in Android are classic Google: overly complicated and requiring great energy and determination to decipher. – Adil Hussain Sep 05 '22 at 18:14
  • For a concrete example of why I think Android's "request permission at runtime" APIs are sub-optimal and lacking in completeness, see my answer [here](https://stackoverflow.com/a/73818832/1071320). – Adil Hussain Sep 23 '22 at 11:31