Reputation: 60869
I would like to know if the user is using the application for the first time. I am using SharedPreferences
, but I am not sure if I have the right logic.
How can I set my isFirstLaunched
boolean to true when the user first launches, and then immediately set to false after work has been done?
protected void onStart() {
super.onStart();
if(isFirstLaunch()){
populateDefaultQuotes();
//Save the preferences, isFirstLaunch will now be false
SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("isFirstLaunch", false);
editor.commit();
}
setupUI();
checkOrientation();
restoreCache();
}
private void populateDefaultQuotes(){
System.out.println("!!!!!! FIRST TIMER !!!!!!");
}
private boolean isFirstLaunch() {
// Restore preferences
SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, 0);
boolean isFirstLaunch = settings.getBoolean("isFirstLaunch", false);
return isFirstLaunch;
}
Upvotes: 2
Views: 1551
Reputation: 513
you can register a BroadcastReceiver to listen for Intent.ACTION_PACKAGE_FIRST_LAUNCH
http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_FIRST_LAUNCH
Upvotes: 0
Reputation: 1108702
Replace the false
argument in getBoolean()
by true
, then the logic will fit.
boolean isFirstLaunch = settings.getBoolean("isFirstLaunch", true);
This will return true
if there is no such setting.
Upvotes: 5