Frage

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?

War es hilfreich?

Lösung

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();

Andere Tipps

You should have a control activity that chooses the layout to display or the activity to run based on that value you saved.

For store options you can look at this and choose the best approach for your case.

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...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top