In-App Update Finish Downloading But The Apps Still The Old Version

713 views Asked by At

so I'm working on a In-Apps Update for my application that already published in Play Store. I follow the documentation and try to test it by lowering my version code then run it, but after finish downloading the app, it supposed to re-open the new version app from but it still re-open my current app which is the lower version. So it's like download for nothing.

I search and got some solution to try like :

  1. installing using apk-release (didn't work)
  2. trying another code for in-app update (still didn't work)

Here's my code :

I put it in my Login page

private val appUpdateManager: AppUpdateManager by lazy { AppUpdateManagerFactory.create(this) }
private val appUpdatedListener: InstallStateUpdatedListener by lazy {
    object : InstallStateUpdatedListener {
        override fun onStateUpdate(installState: InstallState) {
            when {
                installState.installStatus() == InstallStatus.DOWNLOADED -> popupSnackbarForCompleteUpdate()
                installState.installStatus() == InstallStatus.INSTALLED -> appUpdateManager.unregisterListener(this)
                else -> Log.i("TAG", "onStateUpdate: InstallStateUpdatedListener: state: %s"+installState.installStatus())
            }
        }
    }
}

private fun checkForAppUpdate() {
    // Returns an intent object that you use to check for an update.
    val appUpdateInfoTask = appUpdateManager.appUpdateInfo

    // Checks that the platform will allow the specified type of update.
    appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
        if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
            // This example applies an immediate update. To apply a flexible update
            // instead, pass in AppUpdateType.FLEXIBLE
            && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
        ) {
            // Request the update.
            appUpdateManager.startUpdateFlowForResult(
                // Pass the intent that is returned by 'getAppUpdateInfo()'.
                appUpdateInfo,
                // Or 'AppUpdateType.FLEXIBLE' for flexible updates.
                AppUpdateType.IMMEDIATE,
                // The current activity making the update request.
                this,
                // Include a request code to later monitor this update request.
                APP_UPDATE_REQUEST_CODE)
        }
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == APP_UPDATE_REQUEST_CODE) {
        if (resultCode != RESULT_OK) {
            Toast.makeText(this,
                "App Update failed, please try again on the next app launch.",
                Toast.LENGTH_SHORT)
                .show()
        }
    }
}

private fun popupSnackbarForCompleteUpdate() {
    val snackbar = Snackbar.make(
        findViewById(R.id.drawer_layout),
        "An update has just been downloaded.",
        Snackbar.LENGTH_INDEFINITE)
    snackbar.setAction("RESTART") { appUpdateManager.completeUpdate() }
    snackbar.setActionTextColor(ContextCompat.getColor(this, R.color.accent))
    snackbar.show()
}

override fun onResume() {
    super.onResume()
    appUpdateManager
        .appUpdateInfo
        .addOnSuccessListener { appUpdateInfo ->

            // If the update is downloaded but not installed,
            // notify the user to complete the update.
            if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
                popupSnackbarForCompleteUpdate()
            }

            //Check if Immediate update is required
            try {
                if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                    // If an in-app update is already running, resume the update.
                    appUpdateManager.startUpdateFlowForResult(
                        appUpdateInfo,
                        AppUpdateType.IMMEDIATE,
                        this,
                        APP_UPDATE_REQUEST_CODE)
                }
            } catch (e: IntentSender.SendIntentException) {
                e.printStackTrace()
            }
        }
}

Please help me, I already tried to fixing it for 3 days but still have none of the solution are work for me.

0

There are 0 answers