[英]How to clear shared preferences from fragment?
How i can clear shared preferences from fragment?我如何从片段中清除共享首选项? Thank you谢谢
@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);
}
That my fragment那我的片段
you can directly use preference name and clear it from anywhere.您可以直接使用首选项名称并从任何地方清除它。
SharedPreferences preferences = getSharedPreferences("Mypref", 0);
preferences.edit().remove("shared_pref_key").commit();
or或者
SharedPreferences preferences = context.getSharedPreferences("Mypref", Context.MODE_PRIVATE);
preferences .edit().clear().commit();
Use something like this:使用这样的东西:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment_logout, container, false);
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("login.conf", Context.MODE_PRIVATE);
SharedPreferences.Editor 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 view;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.