想要使用 Java 清除 Android 中另一个 activity 的 SharedPreferences

Want to clear SharedPreferences from another activity in Android using Java

所以基本上我制作的计数器在点击按钮后加一。有一个一分钟的倒数计时器。达到 0 后,它将用户导航到下一个 activity,其中计数器数据通过共享首选项传递。问题是,当我关闭应用程序并重新访问 activity 我可以增加计数器的地方时,它会在它停止时开始。所以我想在应用程序关闭后或从另一个 activity 按钮删除共享首选项。这是我的代码:

public class BeginAfter extends AppCompatActivity {

    TextView score1;
    int count = 0;
    String score1f;
    SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_begin_after);
        TextView score1 = findViewById(R.id.score1);
        preferences = getSharedPreferences("score_first", MODE_PRIVATE);

        long duration = TimeUnit.MINUTES.toMillis(1);

        new CountDownTimer(duration, 1000) {
            @Override
            public void onTick(long l) {
                String sDuration = String.format(Locale.ENGLISH, "%01d", TimeUnit.MILLISECONDS.toSeconds(l) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(l)));
                textView.setText(sDuration);
            }

            @Override
            public void onFinish() {
                preferences.edit().remove("image_data").commit();
                Intent intent = new Intent(BeginAfter.this, Begindup.class);{
                    startActivity(intent);
                }
            }
        }.start();
}

    public void correct_button(View view) {
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        int count = preferences.getInt("image_data", 0);
        count++;
        TextView score1 = findViewById(R.id.score1);
        score1.setText("" + count);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putInt("image_data", count);
        edit.commit();
    }

接下来是activity(这对我的问题没用,但以防万一):

public class Begindup extends AppCompatActivity {

    private TextView score1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_begindup);
        score1 = findViewById(R.id.score1);

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        int count = preferences.getInt("image_data", 0);
        score1.setText(String.valueOf(count));
    }

默认情况下,从您的代码中您 int count = 0;,所以与其删除整个 sharedPreference,不如像这样将其更新为 0:

public void correct_button(View view) {
   SharedPreferences.Editor edit = preferences.edit();
   edit.putInt("image_data", 0);//use same key **image_data** change count to 0
   edit.commit();
}

使用相同的键将更新值;

注意:除非您保存计数值以用于应用程序的其他区域,否则我建议您将计数值传递给下一个 activity,Intents 如下:

Int count=405;
Intent intent = new Intent(BeginAfter.this, Begindup.class);    
intent.putInt("key_here", count);
startActivity(intent);

并在接收端(Begindup Activity)在 onCreate 方法中执行此操作:

if (getIntent().getExtras() != null) {
      Int count = getIntent().getIntExtra("key_here");
      //The key argument must always match
}