16

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

NIKUNJ GARG
  • 183
  • 5

4 Answers4

4

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.

headsvk
  • 2,726
  • 1
  • 19
  • 23
1

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!

Rajesh Koshti
  • 572
  • 1
  • 7
  • 25
0

An updated Google doc has been made available: https://developer.android.com/guide/playcore/in-app-updates/kotlin-java

You can launch the update like this:

appUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,
// an activity result launcher registered via registerForActivityResult
activityResultLauncher,
// Or pass 'AppUpdateType.FLEXIBLE' to newBuilder() for
// flexible updates.
AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build())

And the activityResultLauncher is set up in your Activity like this:

private val activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
    val resultCode = result.resultCode
    when {
        resultCode == Activity.RESULT_OK -> {
            Log.v("MyActivity", "Update flow completed!")
        }
        resultCode == Activity.RESULT_CANCELED -> {
            Log.v("MyActivity", "User cancelled Update flow!")
        }
        else -> {
            Log.v("MyActivity", "Update flow failed with resultCode:$resultCode")
        }
    }
}
ProjectDelta
  • 351
  • 4
  • 12
-2

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()


    
shahid17june
  • 1,441
  • 1
  • 10
  • 15
  • 2
    yes I know, this to use with startActivityForResult. But how to use it with startUpdateFlowForResult. Since it takes on 4 parameters with it. https://developer.android.com/reference/com/google/android/play/core/appupdate/AppUpdateManager.html#startUpdateFlowForResult(com.google.android.play.core.appupdate.AppUpdateInfo,%20int,%20android.app.Activity,%20int) – NIKUNJ GARG Oct 27 '21 at 17:51
  • No updates from google play team yet for this as they are saying that it is in their roadmap already so we don't have other option available accept waiting – Rajesh Koshti Aug 05 '22 at 10:42