pileup
pileup

Reputation: 3262

How to implement shouldShowRequestPermissionRationale?

How do I implement the shouldShowRequestPermissionRationale()? So far I am leaving it blank in my code and it works fine, but how can I actually use this method to show my rationale? I understood it should somehow shoe a pop-up dialog with something I write. Or, I can actually do what ever I want there instead of a pop-up dialog? for example show a TextView instead?

This is my code:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {
        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    } else {
        // No explanation needed; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
} else {
    // Permission has already been granted
}

How do I make it show my custom rationale in case the user previously clicked Deny? How do I show my own dialog the second time?

Upvotes: 2

Views: 1640

Answers (1)

Greg Moens
Greg Moens

Reputation: 1825

shouldShowRequestPermissionRationale() is used as a hint that the user may need more info about why you are asking for a particular permission. Typically this method will return true if the user has denied the permission at least once AND they didn't click don't ask again. You can do whatever you want in your UI, most display a popup, but it's up to you. shouldShowRequestPermissionRationale() is not a replacement for requestPermissions(), you'll still need to request permissions after you show your rationale and the user reads it.

Upvotes: 3

Related Questions