Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 991
ofeking109 Avatar asked Nov 27 '22 04:11

ofeking109


1 Answers

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
}
like image 146
ProfSmiles Avatar answered Dec 05 '22 13:12

ProfSmiles