Issue with Sharedpreferences in kitkat version in android

房东的猫 提交于 2019-12-04 06:42:33

问题


Hi in my application I am accepting pass code from user and storing it in shared preferences. And, I have provided pass code on/off functionality. If user checked on , app will ask user to enter pass code at the time of launch every time and will be off on checked of Off button.

Everything is working fine on android's ICS, Jellybean version and below. But it never works on Kitkat. Unfortunately, I don't have a Kitkat device to debug my app. Below is the my code I am using for shared preferences:-

SharedPreferences sharedPreferences;
//sharedPreferences = getActivity().getSharedPreferences("ServerData",         Context.MODE_PRIVATE);
   sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
   Editor editor = sharedPreferences.edit();
   editor.putString("Passcode", Globals.str_Passcode);
   editor.commit();

Does kitkat has any issues related to shared preferences. Please tell me how to resolve this? Or is there any other way to access pass code functionality i same way so it will be accessible in all versions.


回答1:


Try this way,hope this will help you to solve your problem.

SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("Passcode", Globals.str_Passcode);
editor.commit();

sharedPreferences.getBoolean("Passcode",false);



回答2:


I had a similar problem. It was this missing from MainActivity,

prefs = PreferenceManager.getDefaultSharedPreferences(this);
    PreferenceManager.setDefaultValues(this, R.xml.prefs, false);


来源:https://stackoverflow.com/questions/24712821/issue-with-sharedpreferences-in-kitkat-version-in-android

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