How to handle permission never ask again in Kotlin Android
How to handle permission never ask again in Kotlin Android.
Sometimes, when an Android app requests a permission from the user, the user may choose to deny the permission and select the option "Never ask again." This option prevents the app from requesting the permission again, even if the user tries to perform an action that requires that permission. In such cases, it becomes necessary to handle the "Permission Never Ask Again" scenario gracefully. In this tutorial, we will learn how to handle this scenario in Kotlin Android.
Step 1: Requesting the Permission
The first step is to request the necessary permission from the user. This can be done using the requestPermissions()
method in the Activity
or Fragment
class. Here's an example of requesting the Camera
permission:
val CAMERA_PERMISSION_CODE = 1
// Request Camera permission
requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_PERMISSION_CODE)
Step 2: Handling the Permission Result
After the user responds to the permission request, the result is delivered to the onRequestPermissionsResult()
method of the Activity
or Fragment
. You can override this method to handle the permission result. Here's an example:
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
CAMERA_PERMISSION_CODE -> {
// Check if the permission is granted
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, perform the necessary action
openCamera()
} else {
// Permission denied or "Never ask again" selected
if (!shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
// "Never ask again" selected
showPermissionDeniedDialog()
} else {
// Permission denied
showPermissionDeniedSnackbar()
}
}
return
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
In the above example, we check if the permission is granted or denied. If it is denied and the user has selected "Never ask again," we show a dialog explaining the consequences of not granting the permission. If the user has not selected "Never ask again," we show a snackbar requesting the permission again.
Step 3: Handling "Never ask again" Scenario
To handle the "Never ask again" scenario, we need to guide the user to the app settings, where they can manually grant the required permission. Here's an example of how to handle this scenario:
private fun showPermissionDeniedDialog() {
val dialogBuilder = AlertDialog.Builder(this)
dialogBuilder.setMessage("This permission is required to access the camera. Please grant the permission from the app settings.")
.setCancelable(false)
.setPositiveButton("App Settings") { dialog, _ ->
dialog.dismiss()
openAppSettings()
}
.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
}
val dialog = dialogBuilder.create()
dialog.show()
}
private fun openAppSettings() {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivity(intent)
}
In the above example, we show an alert dialog with a message explaining the need for the permission. The positive button in the dialog opens the app settings when clicked, allowing the user to manually grant the permission. The negative button dismisses the dialog.
Conclusion
In this tutorial, we learned how to handle the "Permission Never Ask Again" scenario in Kotlin Android. By requesting the permission, handling the permission result, and guiding the user to the app settings, we can gracefully handle this scenario and ensure that our app functions correctly even without the required permission.