Alternative to onActivityResult for startUpdateFlowForResult

Viewed 466

What is the alternative to get callback for startUpdateFlowForResult in InAppUpdates instead of onActivityResult since it is deprecated?

3 Answers

We have to wait for Google Play team to migrate away from the deprecated APIs. You can follow this issue on Google's Issue Tracker.

You can use below code snippet alternative of onActivityResult() First Activity

Step 1

 private val openActivity =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        handleActivityResult(REQUEST_CODE, it)
    }

Step 2

openActivity.launch(
            Intent(this, YourClass::class.java).apply {
                  putExtra(ANY_KEY, data) // If any data you want to pass 
            }
        )

Step 3

 private fun handleActivityResult(requestCode: Int, result: ActivityResult?) {
        Timber.e("========***handleActivityResult==requestActivty==$requestCode====resultCode=========${result?.resultCode}")
        if (requestCode == REQUEST_CODE) {
            when (result?.resultCode) {
                Activity.RESULT_OK -> {
                val intent =   result.data // received any data from another avtivity
                }
                Activity.RESULT_CANCELED->{
                  
                }
            }
        }
    }

In second class

val intent = Intent()
intent.putExtra(ANY_KEY, data)
setResult(Activity.RESULT_OK, intent)
finish()


    

Create this result launcher

private val updateFlowResultLauncher =
    registerForActivityResult(
        ActivityResultContracts.StartIntentSenderForResult(),
    ) { result ->
        if (result.resultCode == RESULT_OK) {
            // Handle successful app update
        }
    }

After that try to launch your intent like this

 val starter =
        IntentSenderForResultStarter { intent, _, fillInIntent, flagsMask, flagsValues, _, _ ->
            val request = IntentSenderRequest.Builder(intent)
                .setFillInIntent(fillInIntent)
                .setFlags(flagsValues, flagsMask)
                .build()

            updateFlowResultLauncher.launch(request)
        }

    appUpdateManager.startUpdateFlowForResult(
        appUpdateInfo,
        AppUpdateType.FLEXIBLE,
        starter,
        requestCode,
    )

Give it a try!

Related