问题:活动 B 总是返回 false,但是,如果我在活动 A 中检查相同的 KEY,结果为 true。为什么是这样?我检查了 KEY,在这两个活动中它是完全相同的。
编辑:谢谢大家的帮助,但是在实施您的建议后,我仍然没有运气。这是我现在拥有的:
ActivityA, extends Activity Implements OnGesturePerformedListener
//How methods are called...
read(getApplicationContext(),"fav", posName);
write(getApplicationContext(), "fav", posName, false);
...
public Boolean read(Context context, String name, String key) {
SharedPreferences sP = context.getSharedPreferences(name, MODE_PRIVATE);
Boolean b = sP.getBoolean(key, false);
return b;
}
public void write(Context context, String name, String key, boolean value) {
SharedPreferences sP = context.getSharedPreferences(name, MODE_PRIVATE);
SharedPreferences.Editor editor = sP.edit();
editor.putBoolean(key, value);
editor.commit();
}
ActivityB, extends FragmentActivity
, 子类:
public static class SectionFragment extends Fragment
里面onActivityCreated()
SharedPreferences sP = getActivity().getSharedPreferences("fav", MODE_PRIVATE);
Boolean b = sP.getBoolean(posName, false);
结果 =b
总是等于false
有任何想法吗?