Handling permissions in Android11. yes We need this :)
Hi Guys :) I am writing this blog, to sum up, my experience with one weird flow while migrating our apps to Android 11 support.
P.S. I noticed this same behavior in a few of the apps at android11 and it will be mostly there in all till we cater to this specific use case.
For fun just run any of your app on Android11 go to any screen where you ask for permission when the permission dialog comes just click outside of it and observe this behavior :) then cater that this way we talked here. and then feel free to hit the like button.
Let's start with whats weird-
In android 11 I noticed this flow- permissions popups are dismissible, and when user dismiss any permission popup, we get callback like user-selected ‘never ask again’
Let’s see what callbacks are received and take it from start, User is tapping on a button to request Camera permission then once the permission dialog is shown by OS. User is just tapping outside this dialog and dismissing it, then we are getting callback in place of ‘Never ask again’
here a debugging video -
As we can notice when the User is not even denying the permission and just tapping outside this permission dialog to dismiss it still we are getting callbacks like this.
Unfortunately, there is no official android API available to detect if permission is permanently denied when the user selects “Never ask again”, So how we handle this:
we can figure out this state ‘Never ask again’ by checking when permission is already ‘Denied’ and method shouldShowRequestPermissionRationale() returns false
if the user not allowed permission(i.e. denied once previously) and shouldShowRequestPermissionRationale
returns false
then it means the user selected ‘never ask again’ for the permission.
For this, we can create a variable in preferences and make it true when first time user denies a permission
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
setCameraDenied()
showPermissionDeniedDialog(Manifest.permission.CAMERA, REQUEST_CODE_CAMERA)
}
and then we can add a combinational check by adding these both to detect if we can show Mandatory permission needed popup like this -
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) && (AppPreferences.cameraPermissionDeniedOnce)) {
showMandatoryPermissionsNeedDialog()
}
All these conditions combining will be looking like this-
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
REQUEST_CODE_CAMERA -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
findViewById<TextView>(R.id.tvStatus).text = getString(R.string.permission_granted)
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
setCameraDenied()
showPermissionDeniedDialog(Manifest.permission.CAMERA, REQUEST_CODE_CAMERA)
} else {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) && (AppPreferences.cameraPermissionDeniedOnce)) {
showMandatoryPermissionsNeedDialog()
}
}
}
}
}
}
and by adding these checks our permission flow will work as expected.
If you are with me till here, I hope you find it useful :) If you do, feel free to hit the like button. You can find the full code related to this example here.
P.S. I noticed this same behavior in a few of the apps at android11 and it will be mostly there in all till we cater to this specific use case.
For fun just run any of your app on Android11 go to any screen where you ask for permission when the permission dialog comes just click outside of it and observe this behavior :) then cater that this way we talked here. and then feel free to hit the like button.
Here is one example
You can also check my other articles here
Happy Coding :) See you next time till then be you be happy.
For more of our latest learnings on some interesting Android&iOS topics, you can connect to us on our brand new Native Mobile Bits youtube channel or LinkedIn & Github.