using getPreferece() to set a first time run flag

霸气de小男生 提交于 2019-12-24 09:58:45

问题


I have this code that should read an unset preference on the first run:

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    firstTime = settings.getBoolean("firstTime", true);
    Log.d("mything", "firstTime returns as: " + firstTime);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("firstTime", false);
    editor.commit();

the variable "firstTime" is always returned as false. I am uninstalling my app and loading it afresh.

Can someone explain?

Thanks in advance


回答1:


Are you using Samsung Galaxy S with 2.2.1 firmware? There is known bug that shared preferences are not being removed if application is uninstalled. For example see comments here




回答2:


Hmm strange. I run your code and work as expected. The first time you run it is logs

05-10 14:53:59.390: DEBUG/mything(4895): firstTime returns as: true

and if you run it again it always logs

05-10 14:55:25.780: DEBUG/mything(4895): firstTime returns as: false

Are you sure you are not missing something in the log ?



来源:https://stackoverflow.com/questions/5949525/using-getpreferece-to-set-a-first-time-run-flag

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