我如何从片段中清除共享偏好?谢谢
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { sharedPreferences = getActivity().getSharedPreferences("login.conf", Context.MODE_PRIVATE); editor = sharedPreferences.edit(); editor.clear(); editor.commit(); Intent logout = new Intent(getActivity(), LoginActivity.class); startActivity(logout); Log.d(TAG, sharedPreferences.getString("username", "")); Log.d(TAG, sharedPreferences.getString("password", "")); // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_fragment_logout, container, false); }
那是我的片段
您可以直接使用首选项名称并从任何地方清除它.
SharedPreferences preferences = getSharedPreferences("Mypref", 0); preferences.edit().remove("shared_pref_key").commit();
要么
SharedPreferences preferences = context.getSharedPreferences("Mypref", Context.MODE_PRIVATE); preferences .edit().clear().commit();