Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How do I create a function that will be executed only once?

Tags:

android

How can i create a function that will be run only once, on first installation? I am trying to create an information that displays one time.

thanks in advance.

like image 820
Riza Avatar asked Dec 12 '22 17:12

Riza


1 Answers

You could use a flag in the shared preferences.

Then on every startup you check this flag. If it is not set you display your first time message and then set the flag.

For example:

@Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .

       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean firstStart = settings.getBoolean("firstStart", true);

       if(firstStart) {
          //display your Message here

          SharedPreferences.Editor editor = settings.edit();
          editor.putBoolean("firstStart", false);
          editor.commit();
       }
    }
like image 121
RoflcoptrException Avatar answered Jan 21 '23 09:01

RoflcoptrException