Suggest users to update Android app with Google Play (Google In-App Update Support)

Viewed 1497

I saw this dialog always appears when I open old version of Tokopedia app. Tokopedia suggests me to update the app.

enter image description here

The dialog gives me two methods to update the app:

  • Update now
  • Update when Wi-Fi available

If I select Lain Kali (Cancel), the dialog appears again on the next app open. But, if I select the second option, I open Play Store and see this behavior:

enter image description here

It really do update on background until my device is connected to Wi-Fi.

I want to mimic the same action like Tokopedia did, because some version of my app contains critical bug. I want to give users a better user experience.

Do you know how to show the dialog above?

3 Answers

This is possible using In-App Update provided by Google.

In-app updates works only with devices running Android 5.0 (API level 21) or higher, and requires you to use Play Core library 1.5.0 or higher. There are two types - 1.Flexible and 2. Immediate.

Follow this link and implement In-App Update as per your requirement.

https://developer.android.com/guide/app-bundle/in-app-updates

You can achieve this by using Support in-app updates

  • It only work from Android 5.0 (API level 21) or higher.

  • There are two types of Update Available with UX for in-app updates:

    • Flexible
    • Immediate

To Check for update availability

// 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.
    }
});

Okay, here's the complete code as requested by @akshay_shahane.

Firstly, add this line on your app's build.gradle:

dependencies {
    implementation 'com.google.android.play:core:1.6.1'
}

And inside your activity:

class MainActivity : AppCompatActivity(), InstallStateUpdatedListener {

    private val appUpdateManager by lazy {
        AppUpdateManagerFactory.create(this).also { it.registerListener(this) }
    }

    override fun onDestroy() {
        if (Build.VERSION.SDK_INT >= 21) {
            appUpdateManager.unregisterListener(this)
        }
        super.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        if (Build.VERSION.SDK_INT >= 21) {
            appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
                // If the update is downloaded but not installed, notify the user to complete the update.
                if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
                    popupSnackbarForCompleteUpdate()
                } else if (it.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS
                        && it.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                    appUpdateManager.startUpdateFlowForResult(it, AppUpdateType.IMMEDIATE, this, REQUEST_CODE_UPDATE_APP)
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == ActivityResult.RESULT_IN_APP_UPDATE_FAILED && requestCode == REQUEST_CODE_UPDATE_APP) {
            Toast.makeText(this, "Update failed", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onStateUpdate(state: InstallState) {
        when (state.installStatus()) {
            InstallStatus.DOWNLOADED -> popupSnackbarForCompleteUpdate()
            InstallStatus.REQUIRES_UI_INTENT -> {
                Snackbar.make(findViewById(R.id.activity_main_layout),
                        "To perform the installation, a Play Store UI flow needs to be started.",
                        Snackbar.LENGTH_LONG
                ).show()
            }
            else -> {
                val stateString = when (state.installStatus()) {
                    InstallStatus.FAILED -> "failed"
                    InstallStatus.PENDING -> "pending"
                    InstallStatus.DOWNLOADING -> "downloading"
                    InstallStatus.INSTALLING -> "installing"
                    InstallStatus.INSTALLED -> "installed"
                    InstallStatus.CANCELED -> "canceled"
                    else -> null
                }
                if (stateString != null) {
                    Snackbar.make(findViewById(R.id.activity_main_layout),
                            "An update is $stateString.",
                            Snackbar.LENGTH_SHORT
                    ).show()
                }
            }
        }
    }

    private fun popupSnackbarForCompleteUpdate() {
        Snackbar.make(findViewById(R.id.activity_main_layout),
                "An update is ready to install.",
                Snackbar.LENGTH_INDEFINITE
        ).apply {
            setAction("INSTALL") { appUpdateManager.completeUpdate() }
            show()
        }
    }

    @RequiresApi(21)
    fun checkUpdateViaGooglePlay() {
        appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
            when (appUpdateInfo.updateAvailability()) {
                UpdateAvailability.UPDATE_AVAILABLE -> {
                    if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
                        appUpdateManager.startUpdateFlowForResult(
                            appUpdateInfo, AppUpdateType.FLEXIBLE, this, REQUEST_CODE_UPDATE_APP)
                    } else if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                        appUpdateManager.startUpdateFlowForResult(
                            appUpdateInfo, AppUpdateType.IMMEDIATE, this, REQUEST_CODE_UPDATE_APP)
                    }
                }
                UpdateAvailability.UPDATE_NOT_AVAILABLE -> {
                    Toast.makeText(this, R.string.no_updates_found, Toast.LENGTH_SHORT).show()
                }
            }
        }.addOnFailureListener {
            Toast.makeText(this, R.string.error_check_update, Toast.LENGTH_SHORT).show()
        }
    }

    companion object {
        const val REQUEST_CODE_UPDATE_APP = 8
    }
}
Related