Hi all, I have a general query to ask for Activit...
# android
r
Hi all, I have a general query to ask for Activity Contract API. Is there any way, we can write custom Contracts for 3rd Party Libraries that still uses
startActivityForResult
and overrides
onActivityResult
. I am using Google Pay Wallet SDK and it has a function which simply accepts request code and internally calls
startActivityForResult
. Can I do something at my end to make the code more modular using ActivityContracts? Attaching sample code in thread.
😶 6
Copy code
button.setOnClickListener{

AutoResolveHelper.resolveTask(    GoogleClient.googlePaymentsClient.loadPaymentData(request), this,
    REQ_CODE_PAYMENT_DATA
)

}
Copy code
override fun onActivityResult(
  requestCode: Int,
  resultCode: Int,
  data: Intent?
) {
  super.onActivityResult(requestCode, resultCode, data)
  when (requestCode) {
    REQ_CODE_PAYMENT_DATA -> {
      when (resultCode) {
        RESULT_OK -> {
         // do something
        }

        RESULT_CANCELED -> {
         // handle cancel
        }

        AutoResolveHelper.RESULT_ERROR -> {
          //handle error
        }
      }
    }
  }
}
j
If, from Compose, you’re looking to incorporate third-party code that does not yet specify an ActivityResultContract, you can use the general contract:
Copy code
val launcher = rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult(), onResult = { result ->
            /* Do something with result */
        })
🙏 1
Then you simply launch the intent from the onClick:
Copy code
launcher.launch(Intent(context, SomeActivityClass::class.java))
💡 1