What is the alternative to get callback for startUpdateFlowForResult in InAppUpdates instead of onActivityResult since it is deprecated?
Alternative to onActivityResult for startUpdateFlowForResult
Asked Answered
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")
}
}
}
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.
developer.android.com/reference/com/google/android/play/core/… –
Bonitabonito
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!
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
)
}
}
}
© 2022 - 2024 — McMap. All rights reserved.