Android - In-App Updates API Flexible and Immediate Show Update Available Dialog Inside App

Sovary May 20, 2022 7.29K
5 minutes read

As mobile developers we usually want our users to have the most up-to-date version application so that they can try new features, improve performance, and bug fixes if there exist.

Traditionally, users have to go to the Playstore app and update the app manually or automatically when connected to wifi. Today we can learn how to create an app with an update dialog inside our app and update without going to Playstore by using in-app update.

In-App updates feature is supported on devices running Android 5.0 or higher version. we required dependency Play Core Library version 1.5.0 or higher. Moreover, this feature only supported Android mobile devices, Android tablets, and Chrome OS devices.

In-app update has 2 modes of an In-app update Flexible and Immediate.

Requirements

Flexible

  • Mid-dialog pop up shows overlay on the screen
  • Users still continue using the app while download update in the background

flexible in app update android

Immediate

  • Full-screen dialog overlay
  • Users have to wait for download update completely before we can use the app.

immediate in app update android

Implementing FLEXIBLE mode

Step 1: Create a new project in Android Studio (File -> New -> Project -> Select Empty Activty -> Name project AppUpdateTest -> Finish)

Step 2: Add the Google Play Core Dependency to android build.gradle file (Module: app). Note: My latest play core version (video) was 1.7.3 but you can use the latest stable release you want from Play Core Library.

implementation 'com.google.android.play:core:1.7.3'

After we add the library above we have to click on sync link on the top right corner to pull and download the dependency.

Step 3: Open MainActivity.java then we have to instance of the AppManager to get the in-app update info, which is in OnCreate method like the below snippet

AppUpdateManager mAppUpdateManager = AppUpdateManagerFactory.create(this);

Step 4: We will get update information from Playstore and check the update is available or not, before making a request for the update.

mAppUpdateManager.getAppUpdateInfo().addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>()
{
    @Override
    public void onSuccess(AppUpdateInfo result)
    {
        if(result.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                && result.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE))
        {
            try
            {
                mAppUpdateManager.startUpdateFlowForResult(result,AppUpdateType.FLEXIBLE, MainActivity.this
                        ,RC_APP_UPDATE);

            } catch (IntentSender.SendIntentException e)
            {
                e.printStackTrace();
            }
        }
    }
});

mAppUpdateManager.startUpdateFlowForResult(A,B,C,D); for making the request to Playstore to check for updates.

  • A (AppUpdateInfo) is the result update info.
  • B (UpdateAvailability) is the enum value type that refers to FLEXIBLE and IMMEDIATE.
  • C (Activity) is the current context activity to pass.
  • D (Integer) is a random integer number to identify when we use in onActivityResult() method in the next step.

Step 5: After we request and check updates available, it will pop up the dialog. Any way we can choose an action to perform to do next. Example users don't want to update and clicked cancel, we can track those actions by overriding a method onActivityResult() to check as below

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
    /* we can check without requestCode == RC_APP_UPDATE because
    we known exactly there is only requestCode from  startUpdateFlowForResult() */
    if(requestCode == RC_APP_UPDATE && resultCode != RESULT_OK)
    {
        Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

The snippet above is to check when the user clicked cancel or x button.

Step 6: We start monitoring the update in the case below we track when the update is completed download.

private InstallStateUpdatedListener installStateUpdatedListener =new InstallStateUpdatedListener()
{
    @Override
    public void onStateUpdate(InstallState state)
    {
        if(state.installStatus() == InstallStatus.DOWNLOADED)
        {
            showCompletedUpdate();
        }
    }
};

showCompletedUpdate(); we will create the method below to handle the message and install the update.

private void showCompletedUpdate()
{
    Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),"New app is ready!",
            Snackbar.LENGTH_INDEFINITE);
    snackbar.setAction("Install", new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            mAppUpdateManager.completeUpdate();
        }
    });
    snackbar.show();

}

mAppUpdateManager.completeUpdate(); use for install the downloaded update.

Step 7: Remember we have defined a function in Step 6 and we have to activate that function, so let's register the listener in onCreate() method as below.

// Before starting an update, register a listener for updates in onCreate() method
mAppUpdateManager.registerListener(installStateUpdatedListener)

Step 8: We need to unregister the listener, to avoid the memory leak. like the below code:

@Override
protected void onStop()
{
    if(mAppUpdateManager!=null) mAppUpdateManager.unregisterListener(installStateUpdatedListener);
    super.onStop();
}

Yes, now you are completely creating In-App Update with Flexible mode.

Implementing IMMEDIATE mode

Please follow Step 1,Step 2,Step 3

Step 4: We will get update information from Playstore and check the update is available or not, before making a request for the update. the code below is almost the same we changed the update type FLEXIBLE to IMMEDIATE.

mAppUpdateManager.getAppUpdateInfo().addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>()
{
    @Override
    public void onSuccess(AppUpdateInfo result)
    {
        if(result.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                && result.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE))
        {
            try
            {
                mAppUpdateManager.startUpdateFlowForResult(result,AppUpdateType.IMMEDIATE, MainActivity.this
                        ,RC_APP_UPDATE);

            } catch (IntentSender.SendIntentException e)
            {
                e.printStackTrace();
            }
        }
    }
});

Step 5: Please follow Step 5 in Flexible mode

After all, the IMMEDIATE mode does not require registered listener. This mode will automatically install the update after downloaded completely so we no need to call mAppUpdateManager.completeUpdate(); to install manually.

If the user downloading updates in large file size, they might exit the app. To activate when they back to the app we have to override method onResume()

@Override
protected void onResume()
{
    super.onResume();
    mAppUpdateManager.getAppUpdateInfo().addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>()
    {
        @Override
        public void onSuccess(AppUpdateInfo result)
        {
            if(result.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS)
            {
                try
                {
                    mAppUpdateManager.startUpdateFlowForResult(result,AppUpdateType.IMMEDIATE, MainActivity.this
                            ,RC_APP_UPDATE);
                } catch (IntentSender.SendIntentException e)
                {
                    e.printStackTrace();
                }
            }
        }
    });
}

We are checking if the downloading is progressing then we start to continue download the update file.

Alright, follow the above steps you will successfully implement In-app update in FLEXIBLE and IMMEDIATE mode. The video below will completely guide both FLEXIBLE and IMMEDIATE mode including tricky technique testing.

 

You might Also Like:

Android  Java  Video 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him    

Search