应用更新会从内部存储中删除旧的共享首选项数据

时间:2018-09-22 09:59:06

标签: java android sharedpreferences updates

在我的应用程序中,我有一个由用户生成的对象数据库,然后在用户离开应用程序时使用共享的首选项将其保存到内部存储中。现在,当用户重新打开应用程序时,该数据将被检索并呈现给用户以进行进一步的编辑。 我注意到,当我向我的应用程序发布更新并由用户安装时,所有数据都会丢失。我试图通过使用共享的prefs保存应用程序的当前版本代码,然后将其与当前版本代码进行比较来检索它,以便知道何时是应用程序更新,然后我调用Read&Write数据方法来检索旧数据,但是没有运气关于如何处理此问题的任何想法?

SerializeGLB.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!");
}

1 个答案:

答案 0 :(得分:1)

您的SerializeGLBData.Write函数如何工作?因为通过阅读代码,当您升级时,您只是直接调用CallWriteDataMethods,并根据其中的注释进行调用:

  

///将所有的-empty-数据从GlobalDataHolder写入内部存储器,以避免首次读取错误

您正在用空数据写入内存。您的写入功能是否在将空数据放入其中之前检查数据是否存在?

类似

if(!prefs.contains("your_data_key")) {
   // your code to add data
}