添加数据储存(上一节引导页的问题)

1. 一般的APP都是,最开始启动的时候走引导页,下一次启动的时候就不走引导页,直接就到主页。为了实现这个功能。

这个里面的代码是基于 ViewPager实现引导页 这个来实现的。

创建 Welcome.java 欢迎Activity

public class WelcomeAct extends Activity {

    private boolean isFirstIn = false;
    private static final int TIME = 2000;
    private static final int GO_HOME = 1000;
    private static final int GO_GUIDE = 1001;


    // 接收发送来的数据不同 分别到不同的页面
    private Handler mHandler = new Handler(){
        // 发送 message
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME :
                    goHome();
                    break;

                case GO_GUIDE :
                    goGuide();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.init();
        System.out.println("onCreate");
    }

    private void init() {
        // 存储数据
        SharedPreferences preferences = getSharedPreferences("chengzhier.com", MODE_PRIVATE);
        isFirstIn = preferences.getBoolean("isFirstIn", true);
        if (!isFirstIn) {
            mHandler.sendEmptyMessageDelayed(GO_HOME, TIME);   // 走了第一次了,就直接到主页了
        } else {
            mHandler.sendEmptyMessageDelayed(GO_GUIDE, TIME);  // 第一次走这里
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("isFirstIn", false);  // 保存 isFirstIn = false
            editor.commit();  //提交
        }
    }

    // 跳到主页
    private void goHome() {
        Intent i = new Intent(WelcomeAct.this, MainActivity.class);
        startActivity(i);
        this.finish();
    }

    // 跳到引导页
    private void goGuide() {
        System.out.print("go guide");
        Intent i = new Intent(WelcomeAct.this, Guide.class);
        startActivity(i);
        this.finish();
    }
}
原文地址:https://www.cnblogs.com/shaoshao/p/5911171.html