Search code examples
androidpreferences

using getPreferece() to set a first time run flag


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


Solution

  • 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