简体   繁体   English

通过共享首选项保存活动

[英]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: 我的onCreate()方法:

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

In your onCreate you have the condition if(sharedPreferences!= null) When activity will start sharedPreferences will be null. onCreate您的条件是if(sharedPreferences!= null)活动开始时,sharedPreferences将为null。 You will need to get its object again. 您将需要再次获取其对象。

Using Both apply() and commit() , seems redundant to me, try doing all edits and then use commit() which is synchronous with disk writes. 同时使用apply()commit()对我来说似乎是多余的,请尝试进行所有编辑,然后使用与磁盘写入同步的commit()

Difference in commit() and apply() commit()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 . 问题是您在这里检查sharedpreferences != null但启动时始终为null First initialize it with getApplicationContext().getSharedPreferences("my_Pref", MODE_PRIVATE); 首先使用getApplicationContext().getSharedPreferences("my_Pref", MODE_PRIVATE);对其进行初始化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();
        }

Remove if(sharedPreferences!= null){} this part or try 删除if(sharedPreferences!= null){}这部分或尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM