12

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?

Barrufet
  • 495
  • 1
  • 11
  • 33

3 Answers3

15

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

Dhananjay Suresh
  • 1,030
  • 1
  • 14
  • 28
0

Here is described that is not deprecated

Phantom Lord
  • 572
  • 3
  • 13
  • 3
    ResolvableApiException is not deprecated, onActivityResult is: [here says that is better to use a different approach developer](https://developer.android.com/training/basics/intents/result) – Barrufet Dec 15 '20 at 09:16
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)
yozhik
  • 4,644
  • 14
  • 65
  • 98