user3844782 user3844782 - 2 months ago 12
Android Question

Sharedpreferences clears only partially

I am trying to load the last open activity after the app is closed. The below code does that. Now i try to reset on button click so that it clears the sharedpref file and go back to the mainactivity. But clear() method clears only the values that is passed from mainactivity and does not clear the lastactivity key value from the preference file.So when the reset button is clicked the app freezes with a white screen.Tried to remove() the individual lastactivity key value alone but did not work. Also tried apply() ,instead of using commit().What i am doing wrong.

EDIT: I removed the

finish()
method and passed
intent
to
mainactivity
class. Now on reset click the white screen freeze is gone and loads
mainactivity
. But still the
lastactivity
value is not removed from preferences file. Now after force close and on opening app, it loads
secondactivity
with
defaultName
as values instead of loading
mainactivity
.

SecondActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

TextView textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText("Spinner 1");
TextView textView2 = (TextView) findViewById(R.id.textView2);
textView2.setText("Spinner 2");

SharedPreferences prefs = getSharedPreferences(Dispatcher.PREFS_NAME, MODE_PRIVATE);
spinnerValue = prefs.getString("spinnerValue", "defaultName");
spinnerValue2 = prefs.getString("spinnerValue2", "defaultName");

TextView textView5 = (TextView) findViewById(R.id.textView5);
textView5.setText(spinnerValue);
TextView textView6 = (TextView) findViewById(R.id.textView6);
textView6.setText(spinnerValue2);

Button resetButton = (Button) findViewById(R.id.end);
resetButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
finish();
clear();

}

});
}

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

SharedPreferences prefs = getSharedPreferences(Dispatcher.PREFS_NAME, MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
}

public void clear(){
SharedPreferences prefs = getSharedPreferences(Dispatcher.PREFS_NAME, MODE_PRIVATE);
Editor editor = prefs.edit();

//editor.remove("lastActivity");
//editor.commit();

editor.clear();
editor.commit();
}
}


Update:

boolean donotexecutecode = true;

@Override
public void onClick(View view) {
donotexecutecode = false;
clear();
finish();
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences(Dispatcher.PREFS_NAME, MODE_PRIVATE);
if (donotexecutecode) {
Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
}
return;
}
public void clear(){
SharedPreferences prefs = getSharedPreferences(Dispatcher.PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.commit();
}

Answer

Apparently onPause() is called after clear();. So you always put that key. You should use a boolean flag to not execute the code in onPause if you want to force a finish().