开发者

using getPreferece() to set a first time run flag

开发者 https://www.devze.com 2023-03-03 23:25 出处:网络
I have this code that should read an unset preference on the first run: protected void onCreate(Bundle savedInstanceState)

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);
    SharedPreferenc开发者_如何学Ces.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


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


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 ?

0

精彩评论

暂无评论...
验证码 换一张
取 消