I am trying to signout the users when they update the apps. For that setting allowBackup=false, will it clear the shared preferences when the app is updated?
Thank you in advance.
CodePudding user response:
No, allowBackup=false
disables backup only for installation, not for update.
In your case you should save a last version of your app in SharedPrefs and compare with current (on runtime) and decide to clear data or not.
CodePudding user response:
SharedPreferences will not be cleared on an update. allowBackup
will have no effect on that either as it is only used for new installations. You could use SharedPreferences to track the previous version and sign the user out if it no longer matches.
Example:
override fun onCreate(savedInstanceState: Bundle?) {
val sharedPref = getPreferences(Context.MODE_PRIVATE)
val previousVersion = sharedPref.getInt(VERSION_KEY, -1)
val currentVersion = BuildConfig.VERSION_CODE
if (previousVersion != currentVersion) {
// The app has been updated, sign the user out.
sharedPref.edit().putInt(VERSION_KEY, currentVersion).apply()
}
}
companion object {
private const val VERSION_KEY = "appVersion"
}