Search code examples
javaandroidin-app-update

in-app update restarting the obsolete version instead of the newly installed version


I want to implement the in-app update. With the following code, it correctly prompts the user to ask him to agree with the update, downloads the code and installs it. But then the system restarts the previously installed version of the application and again goes through the update process. What's wrong with the following code?

In the onCreate() method:

        appUpdateManager = AppUpdateManagerFactory.create(this);
        Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
            if (mPrefs == null) {
                mPrefs = getSharedPreferences("******" + appName, Context.MODE_PRIVATE);
            }
            int updateRequestCounter = mPrefs.getInt("updateRequestCounter", 0);

            if ((updateRequestCounter == 0) || (updateRequestCounter == 5) || (updateRequestCounter % 10 == 0)) {
                if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
                    switch (appUpdateInfo.installStatus()) {
                        case InstallStatus.FAILED: case InstallStatus.REQUIRES_UI_INTENT: case InstallStatus.UNKNOWN:
                            try {
                                appUpdateManager.startUpdateFlowForResult(
                                        appUpdateInfo,
                                        AppUpdateType.IMMEDIATE,
                                        this,
                                        MY_REQUEST_CODE);
                            } catch (IntentSender.SendIntentException e) {
                                e.printStackTrace();
                            }
                            break;
                        default:
                            break;
                    }
                }
                mPrefs.edit().putInt("updateRequestCounter", 0).apply();

            } else {
                mPrefs.edit().putInt("updateRequestCounter", ++updateRequestCounter).apply();
            }
    });

No idea whether it can be the cause: I use this code not with a production version but with an internal test version of the application.


Solution

  • You are missing the update installation part. You need to call appUpdateManger.completeUpdate() to install the update. AppUpdateManager will relaunch the app for you.

    Test the update installation in release version of your app.