Android in-app updates

Steps to follow for Android in-app updates

Google has introduced a new update – the In-App Updates API to the Android system, granting users and android developers greater control over app compatibility. Discover the latest features with our Android In-App Updates: Step-by-Step Guide.
This new API gives two new ways to update the apps: Flexible and Immediate. In a flexible Android in-app update function, it allows Android users to keep using the app even if it is being updated. Though there is a catch, the app downloads the update even if the user is using it. This is recommended to use when there is no major change in your app such as features that don’t change the core functionality of the app. Once the download was completed the user will see a dialog where the user needs to restart the app.

Android App Development Company

flexible - android in app updates

In the
Immediate Android in-app update function, the users will be given a full-screen update message which forcefully updates their app. The user gets a prompt when the user starts an app and they must update it before using the app. This is recommended when the app update affects directly the core functionality like any critical update. 

immediate - android in app updates

Steps to follow for In-app Update Implementation

Step 1: Check for update:

Let’s start with the In-app update implementation, for this, we need to know whether there is any update available in the Play Store. For this, we need to use AppUpdateManager. The java code used for doing this is:

// Creates instance of the manager.

AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(context);
 
// 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(appUpdateInfo -> {

    if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE

          // For a flexible update, use AppUpdateType.FLEXIBLE

          && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) { // Request the update. }

});

The end result contains update availability status. If the app update is ready, then the update is enabled and returns AppUpdateInfo which intent to begin the update. If an in-app update is previously in progress, the status report will be in-update progress update. 

Step 2: Start Update:

If the update is available, then the update starts by using the AppUpdateManager.StartUpdateFlowForReslt(). Use the following code:

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.

    MY_REQUEST_CODE);

This is should not be used for every app update, there must be some preference. And the request must be done only once unless the update fails. If it fails then request again for the update. While performing the update, you can handle the update failure or cancellation by using the onActivityResult() code as:

@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == MY_REQUEST_CODE) {

    if (resultCode != RESULT_OK) {

      log(“Update flow failed! Result code: ” + resultCode);

      // If the update is cancelled or fails,

      // you can request to start the update again.

    }

  }

}

In this code, you will get MY_REQUEST_CODE from the AppUpdateManager. In the OnActivityResult, you will get the request codes as: 

  • RESULT_OK: If the user accepted the request for the update
  • RESULT_CANCELLED: If the user canceled or denied for the update
  • RESULT_IN_APP_UPDATE_FAILED: If this will show some errors while updating. It may be an error from the user side or some other error.

 
Mobile app service

Handling flexible update: This can be done by using the AppUpdateType.FLEXIBLE while starting an update. Once the app was updated, Google Play will not restart the app. To do so use the below code to restart the app.

override fun onStateUpdate(state: InstallState) {

    if (state.installStatus() == InstallStatus.DOWNLOADED) {

        // After the update is downloaded, show a notification

        // and request user confirmation to restart the app.

        popupSnackbarForCompleteUpdate()

    }

    …

}

 

/* Displays the snackbar notification and call to action. */

fun popupSnackbarForCompleteUpdate() {

    Snackbar.make(

        findViewById(R.id.activity_main_layout),

        “An update has just been downloaded.”,

        Snackbar.LENGTH_INDEFINITE

    ).apply {

        setAction(“RESTART”) { appUpdateManager.completeUpdate() }

        setActionTextColor(resources.getColor(R.color.snackbar_action_text_color))

        show()

    }

}

This code will restart the app after the proper installation of the update.

Handling an Immediate update: For this, to start the update use the code AppUpdateType.IMMEDIATE. In the immediate mode, you must handle every aspect of the update. If the user closes or stops the app, the update should occur in the background. If the update appears in the foreground, handle it using the UpdateAvailability.Developer_Triggered_Update_In_Progress state. We used the following code:

// Checks that the update is not stalled during ‘onResume()’.

// However, you should execute this check at all entry points into the app.

override fun onResume() {

    super.onResume()

 

    appUpdateManager

        .appUpdateInfo

        .addOnSuccessListener { appUpdateInfo ->

            …

            if (appUpdateInfo.updateAvailability()

                == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS

            ) {

                // If an in-app update is already running, resume the update.

                manager.startUpdateFlowForResult(

                    appUpdateInfo,

                    IMMEDIATE,

                    this,

                    MY_REQUEST_CODE

                );

            }

        }

}

Step 3: Handle Cancel update:

If the user cancels the update during or before requesting the download, then you should pass the REQUEST_CODE parameter in the StartUpdateFlowForResult method.

fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

   // super.onActivityResult(requestCode, resultCode, data)

   if (requestCode == InAppUpdateConstant.REQUEST_CODE) {

       when (resultCode) {

           Activity.RESULT_OK -> {

               Log.d(TAG, “” + “Result Ok”)

               //  handle user’s approval }

           }

           Activity.RESULT_CANCELED -> {

               {                          

//if you want to request the update again just call checkUpdate()

               }

               Log.d(TAG, “” + “Result Cancelled”)

               //  handle user’s rejection  }

           }

           ActivityResult.RESULT_IN_APP_UPDATE_FAILED -> {

               //if you want to request the update again just call checkUpdate()

               Log.d(TAG, “” + “Update Failure”)

               //  handle update failure

           }

       }

   }

}

 Conclusion:

Explore the implementation of the In-App Update feature in your app, covering both flexible and immediate types. Learn how to request and monitor update status in our comprehensive Android In-App Updates: Step-by-Step Guide.

Krify is the leading Android app development company based in India and the UK have experienced Android developers. We provide an Android app with all the up-to-date features. More for information please contact us.

Scroll to Top