When using registerForActivityResult in a fragment the callback is never called

Viewed 3147

I'm using registerForActivityResult as the StartActivityForResult function is deprecated. So we are migrating toward the new way using registerForActivityResult. This works perfectly in Activities. However when using this in a fragment the callback is never called. I must also mention that the parent activity and other child fragments handles some results in the old way. When I debug the code i see that the onActivityResult or onRequestPermissionsResult of the parent is called but never the callback. This is the code in the parent activity:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    when (resultCode) {
        Activity.RESULT_OK -> {
            when (requestCode) {
                RC_ONBOARDING_COMPLETE -> {
                    HomeActivity.start(this)
                    finish()
                }
                else -> super.onActivityResult(requestCode, resultCode, data)
            }
        }
        else -> super.onActivityResult(requestCode, resultCode, data)
    }
}

The child fragment:

val requestPermissionsLauncher: ActivityResultLauncher<Array<String>> =
    registerForActivityResult(
        ActivityResultContracts.RequestMultiplePermissions()
    ) { grantMap ->
        accessLocationRequested(!grantMap.containsValue(false) && grantMap.isNotEmpty())
    }

and behind a button :

requestPermissionsLauncher.launch(
                arrayOf(
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION
                )
            )

I also use appcompat 1.3.0-rc01 the latest version available. Any ideas why the parent doesnt forward the result to the child? Oh yeah i checked this in the activity and i returns true:

activityResultRegistry.dispatchResult(
        requestCode, Activity.RESULT_OK, Intent()
            .putExtra(EXTRA_PERMISSIONS, permissions)
            .putExtra(EXTRA_PERMISSION_GRANT_RESULTS, grantResults)
    )
1 Answers

I found the sollution. It was not related to the library. In our base implementation of fragments we use:

override fun getLifecycle(): LifecycleRegistry 

This breaks the callback mechanism.

Related