How to clear old preferences when updating Android app?

后端 未结 4 1514
庸人自扰
庸人自扰 2020-12-30 05:32

I have an app on the Google Play market. For various reasons that I won\'t bother going into, I have changed the type of some of my preferences. For example a preference t

4条回答
  •  佛祖请我去吃肉
    2020-12-30 05:54

    final String PREFERENCE_NAME = "my_preference";
    
    final String APP_VERSION_CODE = 6; /*change this each time you want to clear
    preference in updated app.*/
    
    preferences = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE);
    
    if (preferences.getInt(PREFERENCE_VERSION, 0) != APP_VERSION_CODE) {
          preferences.edit().clear().apply();
          preferences.edit().putInt(PREFERENCE_VERSION, APP_VERSION_CODE).apply();
    }
    

提交回复
热议问题