adil developper
adil developper

Reputation: 41

saving activity via shared preferences

I have a problem with saving activity state, I use shared preferences to save info. When I click button, it's saving it into shared preferences and finishes activity:

sharedPreferences = this.getSharedPreferences("my_Pref", Context.MODE_PRIVATE);
    sharedPreferences.edit().putInt(AGE_SCORE, sbAge.getProgress()).apply();
    sharedPreferences.edit().putInt(STATUS_SCORE, spMyStatus.getSelectedItemPosition()).apply();
    if (rbFemaleMe.isChecked())
        sharedPreferences.edit().putInt(SEX_SCORE, 1).apply();
    else if(rbMaleMe.isChecked())
        sharedPreferences.edit().putInt(SEX_SCORE, 2).apply();
    sharedPreferences.edit().commit();
finish();

After closing activity I open it second time but nothing happens,

My onCreate() method:

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_search);
             if(sharedPreferences!= null)
            {
            sharedPreferences = getSharedPreferences("my_Pref", MODE_PRIVATE);

            sbAge.setProgress(sharedPreferences.getInt(AGE_SCORE, 0));
            spMyStatus.setSelection(sharedPreferences.getInt(STATUS_SCORE, 0));
            if(sharedPreferences.getInt(SEX_SCORE, 0) == 1)
                rbMaleMe.isChecked();
            else if (sharedPreferences.getInt(SEX_SCORE, 0) == 2)
                rbFemaleMe.isChecked();
        }

Upvotes: 0

Views: 53

Answers (3)

jack jay
jack jay

Reputation: 2503

Using Both apply() and commit(), seems redundant to me, try doing all edits and then use commit() which is synchronous with disk writes.

Difference in commit() and apply()

    sharedPreferences = this.getSharedPreferences("my_Pref", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
    edit.putInt(AGE_SCORE, sbAge.getProgress());
        edit.putInt(STATUS_SCORE, spMyStatus.getSelectedItemPosition());
        if (rbFemaleMe.isChecked())
            edit.putInt(SEX_SCORE, 1);
        else if(rbMaleMe.isChecked())
            edit.putInt(SEX_SCORE, 2);
        edit.commit();
    finish();

The problem is here you check for sharedpreferences != null but on starting it would always be null. First initialize it with getApplicationContext().getSharedPreferences("my_Pref", MODE_PRIVATE);.

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_search);

            sharedPreferences = getApplicationContext.getSharedPreferences("my_Pref", MODE_PRIVATE);

             if(sharedPreferences != null)
            {

            sbAge.setProgress(sharedPreferences.getInt(AGE_SCORE, 0));
            spMyStatus.setSelection(sharedPreferences.getInt(STATUS_SCORE, 0));
            if(sharedPreferences.getInt(SEX_SCORE, 0) == 1)
                rbMaleMe.isChecked();
            else if (sharedPreferences.getInt(SEX_SCORE, 0) == 2)
                rbFemaleMe.isChecked();
        }

Upvotes: 1

Faisal Naseer
Faisal Naseer

Reputation: 4248

Remove if(sharedPreferences!= null){} this part or try

sharedPreferences = getSharedPreferences("my_Pref", MODE_PRIVATE);
 if(sharedPreferences!= null)
            {
            ....
            }

Upvotes: 0

gprathour
gprathour

Reputation: 15333

In your onCreate you have the condition if(sharedPreferences!= null) When activity will start sharedPreferences will be null. You will need to get its object again.

Upvotes: 1

Related Questions