3

I have below code which I have implemented the InAPPUpdate code successfully, Which is working fine as per the below screenshot. However, in this implementation User can still cancel the InAppUpdate using the (X) close button and also while downloading the app update user can still cancel the update.

My Question is there a way we can ABSOLUTELY force the end-user to update the app? Unless they update I need to stop them from using the app.

N.B: (Please don't ask why we need to force it's not the question for the discussion)

enter image description here

public class MainActivity extends BaseActivity implements TabLayout.OnTabSelectedListener, MainView {

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //check for updates
        validateAppUpdate();
}

//in app updates
    private void validateAppUpdate() {
        // Creates instance of the manager.
        appUpdateManager = AppUpdateManagerFactory.create(getBaseContext());

        // Returns an intent object that you use to check for an update.
        Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();

        // Checks that the platform will allow the specified type of update.
        appUpdateInfoTask.addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() {
            @Override
            public void onSuccess(AppUpdateInfo appUpdateInfo) {

                Log.e("AVAILABLE_VERSION_CODE", appUpdateInfo.availableVersionCode()+"");
                Log.e("AVAILABLE_VERSION_CODE_2", appUpdateInfo.updateAvailability()+"");
                //Log.e("html", "Error loading html/index.html", e);
                 if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                        // For a flexible update, use AppUpdateType.FLEXIBLE
                        && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                    // Request the update.

                    try {
                        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.
                                MainActivity.this,
                                // Include a request code to later monitor this update request.
                                UPDATE_REQUEST_CODE);
                    } catch (IntentSender.SendIntentException ignored) {

                    }
                }else{
                    System.out.println("No Update Available.");
                }
            }
        });

        appUpdateManager.registerListener(installStateUpdatedListener);

    }
    //lambda operation used for below listener
    InstallStateUpdatedListener installStateUpdatedListener = installState -> {
        if (installState.installStatus() == InstallStatus.DOWNLOADED) {
            popupSnackbarForCompleteUpdate();
        }
    };


    private void popupSnackbarForCompleteUpdate() {

        Snackbar snackbar =
                Snackbar.make(
                        findViewById(android.R.id.content),
                        "Update almost finished!",
                        Snackbar.LENGTH_INDEFINITE);
        //lambda operation used for below action
        snackbar.setAction(this.getString(R.string.APP_NAME), view ->
                appUpdateManager.completeUpdate());
        snackbar.setActionTextColor(getResources().getColor(R.color.browner));
        snackbar.show();
    }
mahen3d
  • 7,047
  • 13
  • 51
  • 103
  • Is closing the app or keeping no-op dialog with error after user declines the update bad UX in your opinion? Also how does your app behave when it's opened without internet aka when update cannot be validated? – Pawel Jan 04 '22 at 13:09
  • @Pawel what if its a banking app and app has major security hole and we need all users to upgrade to the newer version ASAP and a must huh? App needs internet to work in anycase. – mahen3d Jan 04 '22 at 23:29
  • All apps I know (including banking) just show undismissable dialog that update is required and only option left is to close it. Regardless of using in-app update api or not user needs to have an ability to cancel the update and do it later if he wishes to do so (for example if they're on metered connection). – Pawel Jan 04 '22 at 23:58
  • @Pawel thats what i am asking, how to do that in the playstore appinupdate with above code? – mahen3d Jan 04 '22 at 23:59

1 Answers1

1

You need to override onActivityResult() in your class which extends Activity like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != RESULT_OK) {
        validateAppUpdate();
    }
}

this will run validateAppUpdate() until the app is updated.

Estevanbs
  • 140
  • 2
  • 13