【问题标题】:How to clear/reset SharedPreference from an Activity using onBackPressed() Method from another Activity?如何使用另一个 Activity 的 onBackPressed() 方法从 Activity 中清除/重置 SharedPreference?
【发布时间】:2020-04-06 11:38:26
【问题描述】:

所以我将 SharedPreference 编码到(活动 1)中,它允许用户选择在应用启动时首先向他们呈现的活动。代码运行良好,但如果用户想要更改他们的选择怎么办?当用户按下后退按钮返回(活动 1)时,它会自动将他们重定向到他们之前选择的活动。当用户单击调用 onBackPressed() 方法的活动 2 上的“后退按钮”时,如何重置/清除(活动 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();
}

活动 2

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

【问题讨论】:

    标签: android-studio android-activity sharedpreferences onbackpressed


    【解决方案1】:

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

    活动一

    我将变量声明为静态

    public static SharedPreferences sharedPref;
    

    然后我更改了“GetPreferences”行代码

    来自

    final SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);
    

    sharedPref = getPreferences(MODE_PRIVATE);
    

    然后我将其他所有内容都保留为相同的“活动 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();
    }
    

    然后在活动 2 和 3 中,我在 onBackPressed() 方法中添加了一行代码

    活动 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);
    }
    

    【讨论】:

    • 很高兴你找到了工作。我很确定问题是由于您使用本地范围的共享首选项。您需要通过 SharedPreferences sharedpreferences = getDefaultSharedPreferences(this); 使用默认存储或通过以下方式指定一个:SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    【解决方案2】:

    只需将重置共享偏好的代码放在活动 2 中即可。onBackPressed 应该会为您返回到上一个活动。

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

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    

    并在两个活动中使用这些首选项

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

    【讨论】:

    • 我已经尝试过了,但它仍然会将我重定向到之前选择的屏幕。我猜 sharedpreference 没有被删除。
    • @Kezxi,我认为这是因为您没有使用默认的共享首选项。每个活动都在加载一个新的偏好存储。例如,您还可以通过SharedPreferences sharedpreferences = getSharedPreferences("MyPrefStore", Context.MODE_PRIVATE); 使用命名商店。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    相关资源
    最近更新 更多