8

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?

Preeti Tiwari
  • 227
  • 3
  • 7
  • I'm having the same issue, how do we implement this now? Have you found the solution? – Barrufet Dec 15 '20 at 08:38
  • No , i am still searching for the solution. If you find any ,please let me know. Thanks. – Preeti Tiwari Dec 15 '20 at 12:12
  • startResolutionForResult() uses onActivityResult to notify it, and we can't replace it to provide an equal solution using the new way of Intents. So I'm keeping the same solution for now! I tried my best but can't find another way. Sorry – Barrufet Dec 16 '20 at 12:35

1 Answers1

25

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.
        }
    }
}
Gustavo Ross
  • 309
  • 3
  • 7
  • Okay. , and if you consider my above code, then how to pass GPS Request? – Preeti Tiwari Jan 30 '21 at 14:27
  • you dont need GPS_REQUEST constant doing by this way, since resolutionForResult contract is created specific to get result of IntentSender. this callback give you a ActivityResult which have your data(Intent) and resultCode(Int). I'm just doing this on callback private val resolutionForResult = fragmentActivity.registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { activityResult -> if (activityResult.resultCode == Activity.RESULT_OK) { startRequestLocationUpdates() } } – Gustavo Ross Feb 01 '21 at 18:44
  • Ok, i got it. Thank you so much Gustavo. one more doubt for AutoResolveHelper.resolveTask(task, requireActivity(), SAVE_TO_WALLET) this one we are sending for SAVE_TO_WALLET request and getting it result in onActivityResult , if (requestCode == SAVE_TO_WALLET) { sendActivityResult(requestCode, resultCode, data) } how can we achieve this using new method? – Preeti Tiwari Feb 02 '21 at 12:48
  • 1
    Didn't work when I initialized callback inside a fragment method, so use class-level declaration only! – t3ddys Mar 31 '21 at 11:29