开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜