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?
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)
}
}