Android首次打开应用介绍界面实现

本文参考ToolbarMenudrawer开源项目实现。他使用的方法是最一般最通用的做法。

基本思想利用sharedpreferences记录用户是否第一次打开,如果第一次打开应用,跳转到介绍Activity,记录sharedpreferences。

SharedPreferences first = PreferenceManager
               .getDefaultSharedPreferences(this);

if (!first.getBoolean("firstTime", false)) {

    Intent intent = new Intent(this, FirstRun.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();

    //set the flag fot firsttime
    SharedPreferences.Editor editor = first.edit();
    editor.putBoolean("firstTime", true);
    editor.commit();
}  

看到这里或许会有个疑惑,为什么已经调用了finish()方法,下面三句设置SharedPreferences的代码还能生效。经过查询Android中在调用Activity的finish方法,程序会继续执行到finish遇到的一个块结束,也就是“}”括号结束。参考:http://stackoverflow.com/a/6750685/1820217
如果想要强制结束Activity可以在finish之后添加return语句。

Related Articles

0 评论 :

发表评论

Quote Of The Day