如何使用来自另一个 Activity 的 onBackPressed() 方法 clear/reset 来自 Activity 的 SharedPreference?

How to clear/reset SharedPreference from an Activity using onBackPressed() Method from another Activity?

所以我将 SharedPreference 编码为 (Activity 1),它允许用户选择在应用程序启动时首先向他们显示哪个 activity。该代码工作正常,但如果用户想要更改他们的选择怎么办?当用户按下后退按钮返回 (Activity 1) 时,它会自动将他们重定向到他们之前选择的 activity。当用户单击 Activity 2 上的 "back button" 调用 onBackPressed() 方法时,我如何 reset/clear (Activity 1) 中的 sharedpreference?

Activity 1

final SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);
    int choice = sharedPref.getInt("default_activity", -1);


    if (choice == -1) {
        // show the option to choose the default activity to the user
        // e.g. dialog with list, then save the corresponding choice to
        // shared preference
        String[] activities = { "Activity 1", "Activity 2", "Activity 3" };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setAdapter(
                new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, activities),
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences.Editor editor = sharedPref.edit();
                        editor.putInt("default_activity", which);

                        editor.apply();
                        launchActivity(which);
                    }
                }).show();
    } else {
        // start the activity and close this activity
        launchActivity(choice);
    }


}

private void launchActivity(int choice) {
    switch (choice) {
        case 0:
            startActivity(new Intent(this, Activity_1.class));
            break;
        case 1:
            startActivity(new Intent(this, Activity_2.class));
            break;
        case 2:
            startActivity(new Intent(this, Activity_3.class));
            break;
    }
    finish();
}

Activity 2

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent i = new Intent (getApplicationContext(), PreActivity.class);
    startActivity(i);
}

只需将重置共享首选项的代码放在 activity 2. onBackPressed 应该为您返回到之前的 activity。

此外,您可能希望使用默认的共享首选项来确保您正在访问相同的首选项存储:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

并在两项活动中使用这些偏好

 @Override
 public void onBackPressed() {
    PreferenceManager.getDefaultSharedPreferences(this).edit()
              .remove("default_activity")
              .apply()
   super.onBackPressed();
 }

经过数小时的研究和尝试,我找到了解决方案。

Activity 1

我将变量声明为 Static

public static SharedPreferences sharedPref;

然后我更改了"GetPreferences"行代码

来自

final SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);

sharedPref = getPreferences(MODE_PRIVATE);

然后我保持其他一切不变"Activity 1"

 sharedPref = getPreferences(MODE_PRIVATE);
 int choice = sharedPref.getInt("default_activity", -1);


if (choice == -1) {
    // show the option to choose the default activity to the user
    // e.g. dialog with list, then save the corresponding choice to
    // shared preference
    String[] activities = { "Activity 1", "Activity 2", "Activity 3" };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setAdapter(
            new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, activities),
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SharedPreferences.Editor editor = sharedPref.edit();
                    editor.putInt("default_activity", which);

                    editor.apply();
                    launchActivity(which);
                }
            }).show();
} else {
    // start the activity and close this activity
    launchActivity(choice);
   }
}



private void launchActivity(int choice) {
    switch (choice) {
        case 0:
            startActivity(new Intent(this, Activity_1.class));
            break;
        case 1:
            startActivity(new Intent(this, Activity_2.class));
            break;
        case 2:
            startActivity(new Intent(this, Activity_3.class));
            break;
    }
    finish();
}

然后在 Activity 2 & 3 我在 onBackPressed() 方法中添加了一行代码

Activity 2 & 3

public void onBackPressed() {
        super.onBackPressed();
        Activity_2.sharedPref.edit().clear().commit(); // This line references 
                                      //"sharedPref" from the activity (Activity 2)
        Intent i = new Intent(getApplicationContext(), Activity_1.class);
        startActivity(i);
}