onActivityResult is deprecated how do I send startResolutionForResult

Viewed 1932

So I'm using a gps class to turn on the gps and here's a method that allow us to send to onActivityResult, but since is deprecated on AndroidX and still available, android team recommend us the next:

it is strongly recommended to use the Activity Result APIs introduced in AndroidX

I have a the following code where I try to send to onActivityResult

val rae = e as ResolvableApiException
rae.startResolutionForResult(context,Constants.GPS_REQUEST)

How do I approach the same thing with the new API?

3 Answers

What I did in Kotlin was:

registerForActivityResult(StartIntentSenderForResult()) { result ->
    if (result.resultCode == RESULT_OK) {
        // Your logic
    }
}.launch(IntentSenderRequest.Builder(rae.resolution).build())

Original answer: https://stackoverflow.com/a/65943223/4625681

Prepearing to listen for activity result:

private val onRequestSmartLockRetrieveCredentialsResultHandler = registerForActivityResult(
        ActivityResultContracts.StartIntentSenderForResult()
    ) { activityResult ->
        if (activityResult.resultCode == AppCompatActivity.RESULT_OK) {
            val credentials: Credential? = activityResult.data?.getParcelableExtra(Credential.EXTRA_KEY)
            //now we can pass credentials somewhere
            Toast.makeText(
                activity, "Retrieve: OK, Credentials: "
                    + credentials?.id + ", pass: " + credentials?.password, Toast.LENGTH_SHORT
            ).show();
        } else {
            Toast.makeText(activity, "Retrieve: FAIL", Toast.LENGTH_SHORT).show();
        }
    }

Starting activity for result:

val intentSenderRequest = IntentSenderRequest
                            .Builder(exception.resolution).build()
                        onRequestSmartLockRetrieveCredentialsResultHandler.launch(intentSenderRequest)
Related