onActivityResult() deprecated for AppCompatActivity

Viewed 5508

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.

3 Answers

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,
    )
}

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.

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