Endi Tóth
Endi Tóth

Reputation: 221

SharedPreferences saving trouble

I am writing an application for Android and i have difficulties with saving the state of the activity. I'm using sharedpreferences to save the visibility of an imageview. I have a boolean named "flag" which is false first. When i click on the button the flag turns to true. I'm saving the true statement and retrive it in the onCreate method. First time it's working fine, my image is visible. But next time when i close the app it's invisible again and the flag is false. Why? I'd like the flag to stay true until I delete the app. Any responses are welcomed. I copy my code and the LogCat.

private static final String DB_INIT = "DB_INIT";
private static final String IMAGE = "IMAGE";
private boolean flag = false;
Button btn;
ImageView imageview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn = (Button)findViewById(R.id.btn);
    imageview = (ImageView)findViewById(R.id.imageview);

    SharedPreferences sp = getApplicationContext().getSharedPreferences(DB_INIT, Context.MODE_PRIVATE);
    Boolean ize = sp.getBoolean(IMAGE, false);

    Log.d("TEST6","CREATE flag "+flag);
    Log.d("TEST6","CREATE ize "+ize);

    if(ize){
        imageview.setVisibility(View.VISIBLE);
    }
    else{
        imageview.setVisibility(View.INVISIBLE);
    }

    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            flag = true;
            imageview.setVisibility(View.VISIBLE);

            Log.d("TEST6","CREATE flag " +flag);

        }
    });
}
@Override
protected void onStop() {
    SharedPreferences sp = getApplicationContext().getSharedPreferences(DB_INIT,Context.MODE_PRIVATE);
    SharedPreferences.Editor et = sp.edit();
    et.putBoolean(IMAGE, flag);
    Log.d("TEST6","STOP flag " +flag);
    et.commit();
    super.onStop();
}  }

And here is my LogCat:

07-20 18:36:10.226: D/TEST6(5453): CREATE flag false

07-20 18:36:10.234: D/TEST6(5453): CREATE ize false

07-20 18:36:27.156: D/TEST6(5453): CREATE flag true

07-20 18:36:39.390: D/TEST6(5453): STOP flag true

07-20 18:36:43.507: D/TEST6(5453): CREATE flag false

07-20 18:36:43.507: D/TEST6(5453): CREATE ize true

07-20 18:36:47.375: D/TEST6(5453): STOP flag false

07-20 18:36:48.937: D/TEST6(5453): CREATE flag false

07-20 18:36:48.937: D/TEST6(5453): CREATE ize false

I don't understand why is false in the second STOP method.

Upvotes: 0

Views: 73

Answers (2)

d0nut
d0nut

Reputation: 2834

When you read in the boolean from the sharedpreferences make sure to set flag again. When the activity stops it's likely setting the IMAGE flag back to false since flag is false.

Upvotes: 2

Ted Hopp
Ted Hopp

Reputation: 234857

You aren't setting the flag field in onCreate. Get rid if the local variable ize and instead just set flag directly:

flag = sp.getBoolean(IMAGE, false);

...

if(flag){
    imageview.setVisibility(View.VISIBLE);
}
else{
    imageview.setVisibility(View.INVISIBLE);
}

Upvotes: 2

Related Questions