1

I have implemented flexible and immediate in-app update flows on my Android app. I fetch the update type from a remote server, and then use if-else to determine which update type to trigger. As of now, I am experiencing two issues.

  1. The flexible update is not providing a background download. Pressing the update button displays a full screen with download progress.
  2. The immediate update does not restart the app after installation. The user needs to manually open the app.

It is important to note that when I implement only the flexible flow, the flow will work correctly, and the background download will work while the user interacts with the app.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    checkForInAppUpdate();
}
private void checkForInAppUpdate() {
    try {
        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
                if (triggerUpdate
                        && appUpdateInfo.updatePriority() > 3
                        && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                    requestImmediateUpdate(appUpdateInfo);
                } else if (triggerUpdate
                        && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
                    requestFlexibleUpdate(appUpdateInfo);
                }
            }
        });

        appUpdateInfoTask.addOnFailureListener(Throwable::printStackTrace);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private void requestFlexibleUpdate(AppUpdateInfo appUpdateInfo) {
    try {
        appUpdateManager.startUpdateFlowForResult(
                appUpdateInfo,
                AppUpdateType.FLEXIBLE,
                this,
                FLEXIBLE_UPDATE_CODE);
    } catch (IntentSender.SendIntentException e) {
        e.printStackTrace();
    }
}
private void requestImmediateUpdate(AppUpdateInfo appUpdateInfo) {
    try {
        appUpdateManager.startUpdateFlowForResult(
                appUpdateInfo,
                AppUpdateType.IMMEDIATE,
                this,
                IMMEDIATE_UPDATE_CODE);
    } catch (IntentSender.SendIntentException e) {
        e.printStackTrace();
    }
}
@Override
protected void onResume() {
    super.onResume();

    appUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> {
        if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
            // If an in-app update is already running, resume the update.
            requestImmediateUpdate(appUpdateInfo);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == IMMEDIATE_UPDATE_CODE && resultCode != RESULT_OK) {
        checkForInAppUpdate(); // if in any case update request is cancelled and failed
    }
}
amo
  • 147
  • 1
  • 3
  • 13
  • I don't really get your "question" here. The flexible flow downloads the update in the background and once it's downloaded wait for user's confirmation to continue with the app update. The immediate opens a new screen and starts downloading the update and install and from my experience always opens the app after the update is finished. – hardartcore Aug 16 '22 at 12:38
  • @hardartcore my question is as follows When going through a flexible in-app update and you press the update button the download has to start in the background while you are still interacting with the app, but with my implementation above after you press update the download starts on the foreground and you can not interact with the app. – amo Aug 16 '22 at 12:44
  • You are starting the flexible update flow, but where do you call complete? This should be handled somewhere like `onResume` but in your case you are handling only immediate flow. – hardartcore Aug 16 '22 at 12:52
  • @hardartcore I am calling complete on the next screen. – amo Aug 16 '22 at 13:14

1 Answers1

2

Removing the Immediate update listener from onResume() and moving it inside my custom checkForInAppUpdate method fixed my use case.

private void checkForInAppUpdate() {
    try {
        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
                if (triggerUpdate
                        && appUpdateInfo.updatePriority() > 3
                        && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                    requestUpdate(appUpdateInfo, AppUpdateType.IMMEDIATE);
                } else if (triggerUpdate
                        && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
                    requestUpdate(appUpdateInfo, AppUpdateType.FLEXIBLE);
                } else if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                    requestUpdate(appUpdateInfo, AppUpdateType.IMMEDIATE);
                }
            }
        });

        appUpdateInfoTask.addOnFailureListener(Throwable::printStackTrace);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
amo
  • 147
  • 1
  • 3
  • 13