问题
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