Reputation: 728
I'm currently programming an app which Shows a first Setup activity.
How can I let this FirstSetup.class activity be started only for the first time. At the end of the Setup, there is a button FinishSetupButton which could for example set a value 1 that says to the MainActivity not to Show the first Setup again.
How is that possible?
Upvotes: 0
Views: 4534
Reputation: 8680
This is what you need to do: check for a certain value in SharedPreferences every time the app starts. If the value doesn’t exist, this is the first time running an app - do what needs doing then save the value to SharedPreferences. If the value is there, it means this isn't the first time app is running. When app is uninstalled, this value will get removed from SharedPreferences, just FYI. Code below needs to be in an Activity.
static final String KEY_IS_FIRST_TIME = "com.<your_app_name>.first_time";
static final String KEY = "com.<your_app_name>";
...
public boolean isFirstTime(){
return getSharedPreferences(KEY, Context.MODE_PRIVATE).getBoolean(KEY_IS_FIRST_TIME, true);
}
...
this is how you would save this to SharedPreferences:
getSharedPreferences(KEY, Context.MODE_PRIVATE).edit().putBoolean(KEY_IS_FIRST_TIME, false).commit();
Upvotes: 2
Reputation: 1706
I would use SharedPreferences to store that data:
public static void setSettingsDone(Context context) {
final SharedPreferences prefs = context.getSharedPreferences("YourPref", 0);
final Editor editor = prefs.edit();
editor.putBoolean("AlreadySetPref", true);
editor.commit();
}
public static boolean isAlreadySet(Context context) {
final SharedPreferences prefs = context.getSharedPreferences("YourPref", 0);
return prefs.getBoolean("AlreadySetPref", false);
}
In the previous activity, I would call the second method. If false is returned, then the user hasn't already set up sot I'll launch the Settings activity, calling the first methodd at the end, otherwise, I'll launch the MainActivity...
Upvotes: 1