1

I have implemented the functionality to show Flexible in-app update dialog when an update is available. But when the user chooses not to update the app, and the app is restarted the dialog appears again. How can I show the dialog again after a few days of the user choosing not to install the update?

EraftYps
  • 778
  • 1
  • 6
  • 18

1 Answers1

2

You can make use of SharedPreferences in Android. Basically when you ask for the flexible update save the date to SharedPreferences and compare the current day with the one stored in shared preference whenever user uses the app to decide when to ask for the next update.

I would suggest you to ask only once as asking multiple times will annoy your users.

 else if (checkVersion.android_current_version!! > versionName.toLong()) {
                    if (SharedPreference(this@WelcomeActivity).getInt(KEY_UPDATE_ASKED) != (checkVersion.android_current_version as Long).toInt()) {
                        appUpdateManager = AppUpdateManagerFactory.create(this@WelcomeActivity)
                        val appUpdateInfoTask = appUpdateManager!!.appUpdateInfo

                        appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
                            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
                                appUpdateManager!!.startUpdateFlowForResult(
                                        appUpdateInfo,
                                        AppUpdateType.FLEXIBLE,
                                        this@WelcomeActivity,
                                        REQUEST_FLEXIBLE_UPDATE)
                            }
                        }
                    }
                }

This piece of code (which you need to modify according to your own code) lets me ask the flexible update only once whenever checkVersion.android_current_version(version I store in Firebase so I can also decide to hide FlexibleUpdates for some versions) is higher than the version installed in user's phone. Your use case requires you to remove the entry condition and checking date difference instead of checking if the update has been asked before.

A helper class for SharedPreferences

class SharedPreference(val context: Context) {
    private val PREFS_NAME = "PREFERENCES"
    val sharedPref: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)

    fun save(KEY_NAME: String, value: String) {
        val editor: SharedPreferences.Editor = sharedPref.edit()

        editor.putString(KEY_NAME, value)

        editor.apply()
    }


    fun getString(KEY_NAME: String): String? {

        return sharedPref.getString(KEY_NAME, null)
    }
}
Prethia
  • 1,183
  • 3
  • 11
  • 26
  • Thanks for the help. Is it advisable to ask the user again after a few days for a flexible update or should I ask them only when there is a significant change in the application for an IMMEDIATE update using clientVersionStalenessDays()? – EraftYps May 26 '20 at 17:35
  • If your application absolutely cannot continue it's duties without an update, then you should go for force update and not flexible update. I assume the way you are doing it, you ask them whenever there is an update in playstore. What I would do, as I explained the answer is to keep the version numbers I want to ask in Firebase and update it only when I want to ask an update. I would say, ask for each version number you feel is necessary only once, then ask again when there is another update. Don't decide it with a constant number of days, as you may ask multiple times for same version – Prethia May 26 '20 at 17:42
  • If they want to update it/if their auto update is open they can always update themselves, they don't need you to constantly remind them, it is just annoying. But it's a business decision in the end – Prethia May 26 '20 at 17:43