https://kotlinlang.org logo
#android
Title
# android
s

Satyam G

06/22/2022, 2:08 PM
Hello, I am using the In-App Update feature in Android (Immediate Update). We already have a method of
Copy code
appUpdateManager.startUpdateFlowForResult(
    info,
    AppUpdateType.IMMEDIATE,
    parentActivity,
    MY_REQUEST_CODE
)
Which triggers the Immediate flow. If you observe here we are passing here the MY_REQUEST_CODE while triggering Update. Once the update is Finished, Cancel we get the callback in OnActivityResult based on that we can take decisions. But we already know onActivityResult is deprecated now and it is recommended to use the Activity Result Api which has a code like
Copy code
var resultLauncher =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            // There are no request codes
            val data: Intent? = result.data
            inAppUpdate?.let {
                inAppUpdate?.onActivityResult(result.resultCode, data)
            }
        } else if (result.resultCode == AppCompatActivity.RESULT_CANCELED) {
            finish()
        }
    }
But How to get the request code in this case ??? Can anyone help here ?
😶 2
👋 1
l

Landry Norris

06/22/2022, 7:16 PM
I don’t think the new API has request code since they restructured how they worked. The old results API used a single onActivityResult, so the request code was necessary to know what started the request. The new library has you set the result callback for each launcher, so you don’t need a code.
So in your lambda, you know that it will only respond to the contract you gave
resultLauncher
t

tseisel

06/22/2022, 7:54 PM
You probably want to create your own implementation of
ActivityResultContract
that uses this API, unless one already exists. Here is the official guide: https://developer.android.com/training/basics/intents/result#custom
i

Ian Lake

06/23/2022, 4:45 AM
FWIW, I had talked about this on Twitter recently: https://twitter.com/ianhlake/status/1532782435447951361 - tl/dr, you should not be using the
startUpdateFlowForResult
version that takes an Activity, nor should you be using the
StartActivityForResult
contract for this API, which uses the
IntentSender
API
👍 1
😌 1
s

Satyam G

06/23/2022, 5:10 AM
Sure will go through this. Thanks all of you for your valuable inputs . Have a productive day
421 Views