#TECH

How to work with in-app update API in Android

We all use a lot of apps on our devices these days. One for shopping, one for banking, others for ordering food, booking tickets, buying groceries and what not. But the moment the app shows “a new update is pending” notification, we mostly ignore it. As a result, we miss downloading the latest and important feature which can further improve the experience of the app. That’s the story from a user’s end.

But on the developer’s side, the story is a bit different. We want all our users to stay updated with the latest version of the app. We imagine users clicking on the “update now” button and start using the new feature almost instantly. But the harsh truth is that only a few users keep their apps updated with new versions. 

The reason is because most of the times, users don’t bother about opening play store to update their apps. In other words, they don’t have the interest or the time to open and update the app until the app stops doing certain tasks which they want to accomplish.

Google has come up with the solution to this problem. It offers an in-app update feature to update the app without interacting/opening the play store. With this feature, a user can download the latest features while using the app. So let’s dive in to see how we can use this feature.

The in-app update is available in Android Play Core library, that shows prompt to the user for an in-app update. There are two different ways in which we can prompt our users that an update is available.

 In-app update API in Android

Flexible Flow 

If the core functionality of the app doesn’t get affected by the released features then you can use the Flexible Update flow. It allows user to download the update in the background. Once downloading is done, user will be asked to restart the app. Doing so will update the app during the restart process. The benefit of using this Flexible flow is that a user can use the app until the download process is finished. So there is no block time while using the app. User can still use the features as the downloading process is being done in the background.

Immediate Flow

If the core functionality of the app is highly dependent on the released features then you can use the Immediate Update flow. In this feature, a dialog will be prompted to ask the user for downloading and installing updates. The user can’t use the app until the downloading, installing and restarting the app. 

Checking if an update is available or not – 

For a good user experience, we should always ask the user to update the app only if updates are available. We can check if an update is available or not from the backend. 

For that you can use the AppUpdateManager, which will give the AppUpdateInfo object that contains the information about if we need to show the update dialog or not. The updateAvailability method returns these values for the update state :

UNKNOWN

UPDATE_AVAILABLE

UPDATE_IN_PROGRESS

DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS

// Creates instance of the manager.
val appUpdateManager = AppUpdateManagerFactory.create(context)

// 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
        // For a flexible update, use AppUpdateType.FLEXIBLE
        && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
    ) {
        // Request the update.
    }
}


Start an update –

Once you have checked for the updates and in case of updates are available then you can use 

startUpdateFlowForResults() to update the app.

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)


While doing update, the result can be handled using onActivityResult() as shown below.

fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent) {
    if (requestCode == MY_REQUEST_CODE){
        if (requestCode != RESULT_OK){
            log("Update flow failed" + resultCode)
            // If the update is cancelled or fails,
            // you can request to start the update again.
        }
    }
}


We will get the following request codes in onActivityResult.

RESULT_CANCELED : This will indicate that user has denied/canceled for the update.

RESULT_OK : This will indicate that user has accepted the update.

RESULT_IN_APP_UPDATE_FAILED : This will indicate that there is some problems occurred during the update.

So now we know how to use in-app update feature to update the app. It’s a good practice to use the Flexible Flow to update the app when there are very few updates and it doesn’t affect the core functionality of your app. However, you have to make a decision on which flow to use as per your requirements and the kind of app you are making. I hope I was clear and concise in my blog and I hope now you have an understanding of how in-app update API work in Android.

Note: This in-app update feature is available for API level 21 or above and is only for the app that are on the play store.

Reference :

Swiggy uses In-App Update API for better adoption

You might also like