Reading SharedPreferences data in Fragment, within FragmentActivity

与世无争的帅哥 提交于 2019-12-05 20:47:39

I think that you are calling

SharedPreferences sp = getActivity().getSharedPreferences("CHECKBOX", 0);

incorrectly in TestFragment2's loadPrefs() ,method

From the docs the first param (name) is:

Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).

So it looks like you are creating a new prefernces file called "CHECKBOX" in TestFragment2.

maybe change loadPrefs() to the same usage found in Settings.java so you can call up the same preferences used earlier like so:

private void loadPrefs() {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity())
    boolean cbValue = sp.getBoolean("CHECKBOX", false);
    String name = sp.getString("NAME", "YourName");
    if(cbValue){
        cb.setChecked(true);
    }else{
        cb.setChecked(false);
    }
    et.setText(name);

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!