开发者

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

开发者 https://www.devze.com 2023-01-27 03:03 出处:网络
How can i create a function that will be run only once, on first installation? I am trying to create an 开发者_如何学JAVAinformation that displays one time.

How can i create a function that will be run only once, on first installation? I am trying to create an 开发者_如何学JAVAinformation that displays one time.

thanks in advance.


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

精彩评论

暂无评论...
验证码 换一张
取 消