Alternative to onActivityResult for startUpdateFlowForResult
Asked Answered
M

4

20

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

Maestro answered 27/10, 2021 at 12:54 Comment(0)
J
5

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")
        }
    }
}
Jollity answered 15/8, 2023 at 9:37 Comment(0)
B
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.

Brasilein answered 20/5, 2022 at 13:52 Comment(1)
W
2

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!

Wahhabi answered 5/8, 2022 at 10:54 Comment(0)
N
0

This is for Kotlin only. just past the function and there arguments.

private fun checkAppUpdate() {
    // Creates instance of the manager.
    val appUpdateManager = AppUpdateManagerFactory.create(this@MainActivity)

    // Returns an intent object that you use to check for an update.
    val appUpdateInfoTask = appUpdateManager.appUpdateInfo

    val updateOptions = AppUpdateOptions.newBuilder(AppUpdateType.FLEXIBLE).build()

    val updateFlowResultLauncher = registerForActivityResult(
        ActivityResultContracts.StartIntentSenderForResult(),
    ) { result ->

        if (result.resultCode == RESULT_OK) {
            // Handle successful app update
            displayMessage(resources.getString(R.string.app_update_success))
        }

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

 
    
    // Checks that the platform will allow the specified type of update.
    appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
        if (appUpdateInfo.updateAvailability() == UPDATE_AVAILABLE
            // For a flexible update, use AppUpdateType.FLEXIBLE
            && appUpdateInfo.isUpdateTypeAllowed(IMMEDIATE)
        ) {
            // Request the update.
            appUpdateManager.startUpdateFlowForResult(
                appUpdateInfo,
                starter,
                updateOptions,
                Constants.requestCodeForUpdateApp,  //this is int code for request
            )
            
        }
    }

}
Nymphet answered 9/4 at 5:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.