Home > Net >  Want to clear SharedPreferences from another activity in Android using Java
Want to clear SharedPreferences from another activity in Android using Java

Time:09-23

So basically I made counter which is increasing by one after button is clicked. There is one minute countdown timer. After it reaches 0, it navigates user to next activity where counter data is passed with shared preferences. Problem is that when I close the app and revisit activity where i can increase counter, it's starting were it left off. So I want to remove shared preferences after app is closed or from another activity with button. Here's my code:

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, "d", 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();
    }

And this is next activity(it's useless for my question but just in case):

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

CodePudding user response:

From your code you int count = 0; by default, so instead of removing the entire sharedPreference why don't you rather update it to 0 like so:

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();
}

Using same key will update the value;

Note: Unless you save the count value for use in other areas of the application I will suggest you pass the count value to the next activity with Intents like this:

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

And at the receiving end (Begindup Activity) do this in the onCreate method:

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