[英]App update deletes old shared prefs data from internal storage
在我的應用程序中,我有一個由用戶生成的對象數據庫,然后在用戶離開應用程序時使用共享首選項保存到內部存儲中。 現在,當用戶重新打開應用程序時,該數據將被檢索並呈現給用戶以供進一步編輯。 我注意到當我對我的應用程序推出更新並且用戶安裝它時,所有數據都丟失了。 我嘗試通過使用共享首選項保存應用程序的當前版本代碼來檢索它,然后將其與當前版本代碼進行比較以了解它何時是應用程序更新,然后我調用讀取和寫入數據方法來檢索舊數據但沒有運氣. 關於我應該如何解決這個問題的任何想法?
序列化GLB.java:
public class SerializeGLBData {
/**
* Writes the Global User Box's cardList to the user's internal storage using the Gson
* library so that the user doesn't lose his/her data.
* @param cardList The list to write to the internal storage
* @param context Getting the app's current context
*/
public static void Write(ArrayList<Card> cardList, Context context) {
SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = appPrefs.edit();
Gson gson = new Gson();
String cardsGLBJson = gson.toJson(cardList);
editor.putString("cardsGLB",cardsGLBJson);
editor.apply();
editor.commit();
Log.d("WriteData","Data written successfully!");
}
/**
* Reads the cards list that gets saved when the app closes
* @param context Get the app's current context
* @return Returns an ArrayList of Card Objects containing the card info
*/
public static ArrayList<Card> ReadCards(Context context) {
SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Gson gson = new Gson();
String cardsGLBJson = appPrefs.getString("cardsGLB","");
Type type = new TypeToken<ArrayList<Card>>(){}.getType();
return gson.fromJson(cardsGLBJson,type);
}
}
private void checkForFirstRun() {
final String PREF_VERSION_CODE_KEY = "version_code";
final int DOESNT_EXIST = -1;
// Get current version code
int currentVersionCode = BuildConfig.VERSION_CODE;
// Get saved version code
SharedPreferences prefs = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);
// Check for first run or upgrade
if(currentVersionCode == savedVersionCode) {
// This is just a normal run
Log.d("RUN_TYPE:" , "Normal Run");
} else if(savedVersionCode == DOESNT_EXIST) { // This is a new install(or the user cleared the shared prefs)
CallWriteDataMethods(this);
Log.d("RUN_TYPE:", "New Install");
// Showing the tutorial page when the app starts for the first time
Intent tutorialIntent = new Intent(this, Tutorial.class);
startActivity(tutorialIntent);
UsernameDialog dialog = new UsernameDialog();
dialog.setCancelable(false);
dialog.show(getFragmentManager(),"USERNAME_DIALOG");
} else if(currentVersionCode > savedVersionCode) { // This is an upgrade
CallWriteDataMethods(this);
Log.d("RUN_TYPE:","Update");
}
// Update the shared prefs with the current version code
prefs.edit().putInt(PREF_VERSION_CODE_KEY,currentVersionCode).apply();
return;
}
public static void CallWriteDataMethods(Context context) {
// Write all the -empty- data from GlobalDataHolder to the internal memory to avoid a first time read error
SerializeGLBData.Write(GlobalDataHolder.cards,context);
// Write all the -empty- data from JPDataHolder to the internal memory to avoid a first time read error
SerializeJPData.Write(JPDataHolder.cards,context);
}
/**
* Calls every available Read method to retrieve all available data from the GLB database
*/
public static void callReadDataMethodsGLB(Context context) {
GlobalDataHolder.cards = SerializeGLBData.ReadCards(context);
Log.i("Read Methods[GLB]", "ReadMethods called!");
}
/**
* Calls every available Read method to retrieve all available data from the JP database
*/
public static void callReadDataMethodsJP(Context context) {
JPDataHolder.cards = SerializeJPData.ReadCards(context);
Log.i("Read Methods[JP]", "ReadMethods called!");
}
您的 SerializeGLBData.Write 函數如何工作? 因為通過閱讀您的代碼,當您在升級的情況下,您只是直接調用 CallWriteDataMethods,並根據您在其中的注釋:
// 將 GlobalDataHolder 中的所有 -empty- 數據寫入內部存儲器以避免首次讀取錯誤
您正在用空數據寫入內存。 您的編寫函數是否在將空數據放入之前檢查數據是否存在?
所以像
if(!prefs.contains("your_data_key")) {
// your code to add data
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.