Activity that is only launched once after a new install?

不打扰是莪最后的温柔 提交于 2019-11-27 14:25:45

问题


I want my app to have an activity that shows instruction on how to use the app. However, this "instruction" screen shall only be showed once after an install, how do you do this?


回答1:


You can test wether a special flag (let's call it firstRun) is set in your application SharedPreferences. If not, it's the first run, so show your activity/popup/whatever with the instructions and then set the firstRun in the preference.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences settings = getSharedPreferences("prefs", 0);
    boolean firstRun = settings.getBoolean("firstRun", true);
    if ( firstRun )
    {
        // here run your first-time instructions, for example :
        startActivityForResult(
             new Intent(context, InstructionsActivity.class),
             INSTRUCTIONS_CODE);

    }
 }



// when your InstructionsActivity ends, do not forget to set the firstRun boolean
 protected void onActivityResult(int requestCode, int resultCode,
         Intent data) {
     if (requestCode == INSTRUCTIONS_CODE) {
         SharedPreferences settings = getSharedPreferences("prefs", 0);
         SharedPreferences.Editor editor = settings.edit();
         editor.putBoolean("firstRun", false);
         editor.commit();
     }
 }



回答2:


yes, you can fix this problem with SharedPreferences

SharedPreferences pref;
SharedPreferences.Editor editor;

pref = getSharedPreferences("firstrun", MODE_PRIVATE);
editor = pref.edit();

editor.putString("chkRegi","true");
editor.commit();

Then check String chkRegi ture or false



来源:https://stackoverflow.com/questions/8779929/activity-that-is-only-launched-once-after-a-new-install

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!