Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActivityResultCallback not invoked for RequestMultiplePermissions

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?

  • Library version: androidx.activity:activity-ktx:1.2.0-alpha06
  • MinSdkVersion: 21
  • TargetSdkVersion: 29
like image 287
Jakub Gruber Avatar asked Jan 25 '26 12:01

Jakub Gruber


1 Answers

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.

like image 83
ArchetypeAnarch Avatar answered Jan 27 '26 00:01

ArchetypeAnarch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!