Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deprecated OnActivityResult() in androidx

OnActivityResult() is deprecated in androidx. I took reference from below links

  • https://developer.android.com/training/basics/intents/result
  • https://developer.android.com/jetpack/androidx/releases/activity
  • https://proandroiddev.com/is-onactivityresult-deprecated-in-activity-results-api-lets-deep-dive-into-it-302d5cf6edd

I implemented for signup thing in my project ..like .I created resultcontract by registering the activity in place of startActivityForResult i replaced by resultcontract.launch(intent) and getting result in resultcontract (followed above links). Now i have doubt in one function that is turn on gps. for this we have used

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

earlier it was startActivityForResult , now it is startResolutionForResult having confusion how to get result of gps request ( in code , we are getting result in onActivityResult).how can i implement this using new way?

like image 702
Preeti Tiwari Avatar asked Dec 05 '20 14:12

Preeti Tiwari


People also ask

What can I use instead of Startactivity for results?

But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it. Step 1: You just have to create an ActivityResultLauncher and pass following parameters handle onActivityResult in it as shown above.

What is start activity for result?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

In which API version Startactivity for result is deprecated?

activity:activity-ktx to 1.2. 0 . It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.

Is start activity for result deprecated?

onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.


1 Answers

I found a solution for this problem, using the recent API's to start something for result.

So, you can pretty much get the resolution from ResolvableApiException, which is a PendingIntent, and start it with StartIntentSenderForResult contract, like this

contract:

private val resolutionForResult = registerForActivityResult(
    ActivityResultContracts.StartIntentSenderForResult()
) { activityResult ->

    // do whatever you want with activity result...

}

how to start:

.addOnFailureListener { exception ->
    if (exception is ResolvableApiException) {
        try {
            val intentSenderRequest = IntentSenderRequest
                    .Builder(exception.resolution).build()
            resolutionForResult.launch(intentSenderRequest)
        } catch (throwable: Throwable) {
            // Ignore the error.
        }
    }
}
like image 99
Gustavo Ross Avatar answered Nov 04 '22 21:11

Gustavo Ross