Android-How can I know if it is the first time the application launched?

ofeking109 picture ofeking109 · Dec 23, 2011 · Viewed 8.2k times · Source

How can I know if it is the first time the application launched?

If you are answering please add a full code because I have read some answers and I didn't understand them.

Thanks.

Answer

ProfSmiles picture ProfSmiles · Dec 23, 2011

Every app gets a way to store preferences or options, so you can have one for whether or not the app has previously run

SharedPreferences runCheck = PreferenceManager.getSharedPreferences("hasRunBefore", 0); //load the preferences
Boolean hasRun = runCheck.getBoolean("hasRun", false); //see if it's run before, default no
if (!hasRun) {
    SharedPreferences settings = getSharedPreferences("hasRunBefore", 0);
    SharedPreferences.Editor edit = settings.edit();
    edit.putBoolean("hasRun", true); //set to has run
    edit.commit(); //apply
    //code for if this is the first time the app has run
}
else {
    //code if the app HAS run before
}