onActivityResult is deprecated how do I send startResolutionForResult
Asked Answered
W

3

15

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?

Wideawake answered 15/12, 2020 at 8:47 Comment(5)
#62671606Truckload
Since I'm accessing from a custom class that is "far from activity", I do need to make variable launcher public, access to activity variable, and then call it, is that a good practice?Wideawake
no issues. you can do thisTruckload
and so... how do I launch this?Wideawake
I found the answer here: https://mcmap.net/q/824699/-deprecated-onactivityresult-in-androidxSchoonover
S
17

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://mcmap.net/q/824699/-deprecated-onactivityresult-in-androidx

Schoonover answered 12/3, 2021 at 23:4 Comment(0)
H
0

Here is described that is not deprecated

Hilmahilt answered 15/12, 2020 at 9:7 Comment(1)
ResolvableApiException is not deprecated, onActivityResult is: here says that is better to use a different approach developerWideawake
A
0

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)
Archon answered 13/7, 2022 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.