Mikas Mikas - 2 months ago 6x
Android Question

SharedPreferences code not working as intended

All necessary imports are added.

public class MainActivity extends AppCompatActivity {

SharedPreferences preferences;
SharedPreferences.Editor editor;
public int i;

public void onCreate(Bundle savedInstanceState) {

preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
editor = preferences.edit();

During first time boot the message "First time boot" appears, but should not appear during next times, instead the iterator i, which was saved in sharedPreferences, should appear.


i = preferences.getInt("iterator", 0);

Toast.makeText(getApplicationContext(), i, Toast.LENGTH_SHORT).show();

}catch(Exception e){
Toast.makeText(MainActivity.this, "First time boot", Toast.LENGTH_SHORT).show();

final TextView date = (TextView) (findViewById(R.id.textView));
final Calendar c = Calendar.getInstance();

final Button mainBtn = (Button) findViewById(R.id.mainBtn);
final Button exit = (Button) findViewById(R.id.exit);

mainBtn.setText("Clock In");

mainBtn.setOnClickListener(new View.OnClickListener(){

public void onClick(View v){
if(mainBtn.getText() == "Clock In"){
mainBtn.setText("Clock Out");
}else{mainBtn.setText("Clock In");}

String s = new SimpleDateFormat("yyyy: MM: dd HH: mm: ss").format(Calendar.getInstance().getTime());

My idea is that upon clicking a button, a time stamp would be saved into the sharedPreferences file, which could later on be accessed.

editor.putString("Time "+ i, s);


editor.putInt("iterator", i);


I just recently added this, so that when you click the exit button, everything that was put in the sharedPreferences file would be saved. (earlier the editor.commit() line was inside the mainBtn onClickListener).

exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

The problem is that upon exiting the app and re-launching it, the app does not retrieve the stored data, which was supposed to be saved in the sharedPreferences file, instead it just says "First time boot" every time.. I am extremely new to programming and have tried to overcome this problem in multiple ways, only to arrive at the same problem. Help!


To my knowledge preferences.getInt("iterator", 0); won't throw an exception if "iterator" doesn't exist. It will return a 0. 0 is the default. I would change your code to be something more like:

    int iterator = preferences.getInt("iterator", -1);
    if (iterator == -1)
      Toast.makeText(this, "First time boot", Toast.LENGTH_SHORT).show();

And if you look at your exception, I would guess that the problem is actual from your Toast after your getInt("Iterator", 0) call that is passing an int instead of a string.

Also, I wouldn't hold the editor open throughout your app. I usually would write a function like

void savePreference(String key, String value) {
    SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, 0);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(key, value);