Reputation: 191
I am using in-app updates for android and as per the documentation, they are using onActivityResult to handle app behaviour incase the update is interrupted.
This is my function that is called from my fragment:
private fun startImmediateUpdate(appUpdateInfo: AppUpdateInfo) {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
requireActivity(),
Constants.CODES.APP_UPDATE_REQUEST_CODE
)
}
This is how i am handling results in parent activity
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
Constants.CODES.APP_UPDATE_REQUEST_CODE -> {
if (resultCode != RESULT_OK || resultCode == RESULT_CANCELED || resultCode == ActivityResult.RESULT_IN_APP_UPDATE_FAILED) {
//Do whatever i want to
}
}
}
super.onActivityResult(requestCode, resultCode, data)
}
Now super.onActivityResult(requestCode, resultCode, data) is deprecated. Things are working fine for now but i am worried the app will crash if its wiped out completely
What can i do to replace onActivityResult()? I have looked into registerForActivityResult() but could not find anything that suits my usecase.
Upvotes: 15
Views: 7145
Reputation: 6995
Update (June 2023):
Since version 2.1.0 of the In-App Updates library, which was released in May 2023, an overload of startUpdateFlowForResult that accepts an IntentSenderForResultStarter
was finally added.
Old answer (before version 2.1.0):
As Ian Lake mentioned there is a solution that uses IntentSenderForResultStarter.
First of all in your Activity, create a ActivityResultLauncher:
private val updateFlowResultLauncher =
registerForActivityResult(
ActivityResultContracts.StartIntentSenderForResult(),
) { result ->
if (result.resultCode == RESULT_OK) {
// Handle successful app update
}
}
Now start the update flow as follows:
fun startUpdate(
appUpdateInfo: AppUpdateInfo,
requestCode: Int,
) {
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,
)
}
Upvotes: 11
Reputation: 14
The New Way
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& (appUpdateInfo.clientVersionStalenessDays() ?: -1) >= 2
&& appUpdateInfo.updatePriority() >= updatePriority
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
) {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
this,
updateCode
)
openActivityForResult()
}
}
}
private fun openActivityForResult(){
resultUpdate.launch(Intent(this, MainActivity::class.java))
}
var resultUpdate = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ result->
if (result.resultCode != Activity.RESULT_OK){
Toast.makeText(
this,
"App Update failed, please try again on the next app launch.",
Toast.LENGTH_SHORT
).show()
}
}
Upvotes: -1
Reputation: 946
I have the same issue and I see that one possibility is to rewrite to use the startUpdateFlow call instead.
Personally, I think this is not worth doing, so I will ignore this specific deprecation warning instead and wait/hope for Android/Google to make a method where we can pass an ActivityResultLauncher
instead.
Upvotes: 5