Situation
One of my fragments accesses the camera. So of course i need to check the permission in my activity first, before i redirect to it. If the user denies the permission the Activity finishes and redirects him to the previous Activity he was in.
Additionally i would like to show a different fragment when the Activity starts and detects that the permission was permanently denied. (The little checkbox "Never ask again" on the android permission dialog)
Problem
I could not find a proper way of detecting, wether the permission was only denied once or if the "Never ask again" checkbox was checked last time and denied the permission permanently.
Keep in mind that i don't want to know that in the onRequestPermissionsResult callback. I need to know in the onCreate of my Activity if the permission currently is granted, denied or permanently denied.
What i tried
ActivityCompat#shouldShowRequestPermissionRationale
seems to detect wether the permission has been denied in the past or not. It also returns true if it has been denied only once instead of permanently.
PermissionChecker#checkPermission()
didn't seem to notice any difference between permanently and only once denied permission state.
Question
Is there any way of detecting, wether a permission is denied but can still be requested or if it is permanently denied?
Unfortunately there is no official API available to detect if permission is permanently denied when user selects "Never ask again"
There is one work around which uses
shouldShowRequestPermissionRationale
. Create a SharedPreference with default valuefalse
and store value returned byshouldShowRequestPermissionRationale
in it. Before updating the value, check if the value set wastrue
. If it wastrue
then don't update it.Whenever you want to check for permission, get the value from
SharedPreference
and current value returned byshouldShowRequestPermissionRationale
. IfshouldShowRequestPermissionRationale
returnsfalse
but value fromSharedPreference
istrue
, you can deduce thatNever ask again
was selected by user.You can refer to my blog where I have described this approach.
You can check the permission by,
Normally I check the permission using following,