Commit 9be45826697ce068c5852e9f00efe0d9c046f636

Authored by shixianjie
1 parent c7efa41793
Exists in master

避免版本升级实体类变化导致崩溃

app/src/main/java/com/hjx/parent/App.java
... ... @@ -39,6 +39,7 @@ public class App extends CommonApplication {
39 39 super.onCreate();
40 40 this.instance = this;
41 41 SharedPreferencesUtil.getInstance(this, "config");
  42 + SharedPreferencesUtil.checkVersion(this);
42 43 ZXingLibrary.initDisplayOpinion(this);
43 44 }
44 45 }
... ...
libs/common/src/main/java/com/prws/common/utils/SharedPreferencesUtil.java
... ... @@ -263,10 +263,19 @@ public class SharedPreferencesUtil {
263 263 }
264 264  
265 265 public static void clear(Context context) {
266   - SharedPreferences preferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
267   - SharedPreferences.Editor editor = preferences.edit();
  266 + SharedPreferences.Editor editor = sp.edit();
268 267 editor.clear();
269 268 editor.commit();
270 269 }
271 270  
  271 + private static final int myVersion = 2;
  272 + public static void checkVersion(Context context) {
  273 + if (sp.getInt("myVersion", 0) == myVersion) {
  274 + return;
  275 + }
  276 + clear(context);
  277 + SharedPreferences.Editor editor = sp.edit();
  278 + editor.putInt("myVersion", myVersion);
  279 + editor.apply();
  280 + }
272 281 }
... ...