新安装后仅启动一次的活动? [英] Activity that is only launched once after a new install?

查看:19
本文介绍了新安装后仅启动一次的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用有一个活动来显示如何使用该应用的说明.但是,这个指令"屏幕在安装后只能显示一次,你怎么做?

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?

推荐答案

您可以测试是否在您的应用程序中设置了特殊标志(我们称之为 firstRun)SharedPreferences.如果没有,这是第一次运行,因此请按照说明显示您的活动/弹出窗口/任何内容,然后在首选项中设置 firstRun.

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

这篇关于新安装后仅启动一次的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆