有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么在Android中使用clear()后,我的SharedReference会在活动中持久存在?

我已经找遍了,似乎找不到我的问题的答案。出于某种原因,SharedPreferences中保存的数据在切换活动后重新出现。为什么我的SharedReferences仍然和我清除它之前一样,我该如何修复它

在第一个活动中:

@Override
protected void onStop() {
    super.onStop();

    SharedPreferences sp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);
    Gson gson = new Gson();

    String objectJson = gson.toJson(object, Object.class);
    String activityJson = "FirstActivity";

    SharedPreferences.Editor editor = sp.edit();
    editor.putString(CONTINUE_KEY, objectJson);
    editor.putString(ACTIVITY_KEY, activityJson);
    editor.apply();
}

public void onClickSave() {

    Gson gson = new Gson();
    String objectJson = gson.toJson(object);
    SharedPreferences sp getSharedPreferences(SAVE_KEY, MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putString(object.getDate(), objectJson);
    editor.apply();

    SharedPreferences spTemp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);
    spTemp.edit().clear().apply();
    // go back to the main menu
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

在应用()之前:

Debugger

在apply()之后:

Debugger

在主要活动中:

SharedPreferences sp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);

主要活动:

Debugger


共 (3) 个答案

  1. # 1 楼答案

    保存到共享首选项

    SharedPreferences sp = getSharedPreferences("test" , MODE_PRIVATE);
    SharedPreferences.Editor spEditor = sp.edit();
    spEditor.putString("hello1" , "hello11");
    spEditor.putString("hello2" , "hello22");
    spEditor.apply();
    

    从共享偏好中清除

    SharedPreferences sp1 = getSharedPreferences("test" , MODE_PRIVATE);
    SharedPreferences.Editor spEditor1 = sp1.edit();
    spEditor1.clear();
    spEditor1.apply();
    

    从共享偏好中获取

    SharedPreferences sp2 = getSharedPreferences("test" , MODE_PRIVATE);
    String value =  sp2.getString("hello1" ,"noting");
    Log.i("test_sp" , " == == ==" +value);
    
  2. # 2 楼答案

    来自Coordinating activitiesandroid文档

    When one activity starts another, they both experience lifecycle transitions. The first activity stops operating and enters the Paused or Stopped state, while the other activity is created. In case these activities share data saved to disc or elsewhere, it's important to understand that the first activity is not completely stopped before the second one is created. Rather, the process of starting the second one overlaps with the process of stopping the first one.

    The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process (app) and one is starting the other. Here's the order of operations that occur when Activity A starts Activity B:

    1. Activity A's onPause() method executes.

    2. Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)

    3. Then, if Activity A is no longer visible on screen, its onStop() method executes.

    This predictable sequence of lifecycle callbacks allows you to manage the transition of information from one activity to another.

    回到你的案例,当你从FirstActivity开始MainActivity

    1. FirstActivityonPause()方法执行

    2. MainActivityonCreate()onStart()onResume()方法按顺序执行。(MainActivity现在以用户为中心。)

    3. 然后,如果FirstActivity在屏幕上不再可见,则执行其onStop()方法

    onClickSave中的代码执行时

    SharedPreferences spTemp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);
    spTemp.edit().clear().apply();
    

    此时,TEMP_KEYprefs已被清除。当MainActivity在屏幕上可见后,FirstActivity将不再在屏幕上可见,因此它的onStop()方法将执行

    SharedPreferences sp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);
    Gson gson = new Gson();
    
    String objectJson = gson.toJson(object, Object.class);
    String activityJson = "FirstActivity";
    
    SharedPreferences.Editor editor = sp.edit();
    editor.putString(CONTINUE_KEY, objectJson);
    editor.putString(ACTIVITY_KEY, activityJson);
    editor.apply();
    

    在这段代码中,您再次将两个条目添加到TEMP_KEYprefs中。因此,当用户单击MainActivity中的按钮时,此时prefs大小是2,而不是您预期的0

    Why is my SharedPreferences still the exact same as it was before I cleared it?

    这在安卓系统中是可预测或预期的行为。你可以在MainActivity方法的^{onCreate()onStart()onResume()中放置一个日志,然后你可以看到TEMP_KEYprefs大小在当时总是0

    How can I fix this?

    不要在^{onStop()中的TEMP_KEY中添加新条目,而是应该在onPause()中添加新条目

  3. # 3 楼答案

    首先,您对getSharedPreferences(String name, int mode)、在onStop()onClickSave()方法中有不同的首选文件名:

    // in onStop()
    SharedPreferences sp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);
    
    // in onClickSave()
    SharedPreferences sp getSharedPreferences(SAVE_KEY, MODE_PRIVATE);
    

    您需要使用相同的文件名来访问相同的首选项

    SecondonStop()并不总是保证被调用,因此它不是保存首选项的可靠方法。相关答案:Is onStop() always preceded by onPause()

    第三个,当需要保存首选项中的值时,请始终保存首选项。不要等到以后再保存它,希望你可以依赖onPause()onStop()onDestroy()。永远不要认为你的应用程序会被用户或系统优雅地终止