Android: Fragment onActivityResult() isn't called when using In-app update

1.2k views Asked by At

For the past couple of days, I'm trying to implement the immediate in-app update functionality, which works fine, but it seems I'm being unable to handle the result code (e.g. user cancels the update) in fragment - onActivityResult() as the method isn't called at all.

After some fiddling I realized, that after refusing the update, the onActivityResult() is called, but for the parent activity, not the fragment where I need to handle the result, which makes sense.

I'm using the code from the default example, this is just an fragment excerpt, as the code is wrapped in Rx observer logic.

appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
    appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.IMMEDIATE,
        fragment.activity, UPDATER_REQUEST_CODE)
}


// Trying to handle the in-app update activity result in here with no luck
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == UPDATER_REQUEST_CODE) {
        // Log onActivityResult() result code
    }
}

Is there by any chance somebody, who stumbled upon the same problem?

Thanks guys a lot in advance.

1

There are 1 answers

0
zadek On BEST ANSWER

I found the solution after diving deeper into the documentation, where it is stated, that method startUpdateFlowForResult() with different arguments should be used.

https://developer.android.com/reference/com/google/android/play/core/appupdate/AppUpdateManager

This method should be called if you are starting the flow from a Component different from an Activity and you don't want to receive the onActivityResult call on the Activity.

For example, you can use it for androidx.fragment.app.Fragment:

So the trick is to use this

startUpdateFlowForResult(appUpdateInfo, AppUpdateType.IMMEDIATE, fragment::startIntentSenderForResult, UPDATER_REQUEST_CODE)

instead of this

startUpdateFlowForResult(appUpdateInfo, AppUpdateType.IMMEDIATE, fragment.activity, UPDATER_REQUEST_CODE)