I followed this tutorial to implement a new approach to request application permissions via Results API by RequestMultiplePermissions contract. Although the permission dialog is shown and permission result is propagated through the system to application preferences etc., my provided ActivityResultCallback is not notified at all.
Here are my source codes. I am aware I am not checking whether the user hasn't declined the permission already:
private fun checkPermissions() {
val permissionList = arrayOf(
Manifest.permission.CAMERA,
Manifest.permission.ACCESS_FINE_LOCATION
)
val notGrantedPermissions = permissionList.map {
Pair(
it, ContextCompat.checkSelfPermission(
applicationContext,
it
)
)
}.filter {
it.second != PackageManager.PERMISSION_GRANTED
}
.map { it.first }
.toTypedArray()
if (notGrantedPermissions.isEmpty()) {
nextActivity()
} else {
requestPermissionLauncher.launch(notGrantedPermissions)
}
}
private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { result ->
info("> requestPermissionLauncher - ${result.values}")
if (result.values.all { it }) {
nextActivity()
} else {
longToast("All permissions are required for app to work correctly")
checkPermissions()
}
}
Did I miss anything in the documentation?
I was up against this same issue tonight. I'm betting you're either extending AppCompatActivity or something similar. The issue with that is, when super.onRequestPermissionsResult is invoked, it falls to the FragmentActivity implementation which does not, itself, invoke the super method so the chain dies there. The quick solution is to extend ComponentActivity directly. However, if this is not feasible for your solution, you can override the method as follows:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
activityResultRegistry.dispatchResult(requestCode, Activity.RESULT_OK, Intent()
.putExtra(EXTRA_PERMISSIONS, permissions)
.putExtra(EXTRA_PERMISSION_GRANT_RESULTS, grantResults))
}
The above is a direct port from the ComponentActivity method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With