引导页是否是首次进入

不废话了,先上代码

首先先判断是否是第一次进入,然后分别做处理

boolean mFirst = isFirstEnter(SplashActivity.this,SplashActivity.this.getClass().getName());
if(mFirst)
    mHandler.sendEmptyMessageDelayed(Constant.SWITCH_GUIDACTIVITY,100);
else
    mHandler.sendEmptyMessageDelayed(Constant.SWITCH_MAINACTIVITY,100);
private boolean isFirstEnter(Context context, String className){
    if(context==null || className==null||"".equalsIgnoreCase(className))return false;
    String mResultStr = context.getSharedPreferences(Constant.SHAREDPREFERENCES_NAME, MODE_PRIVATE)
            .getString(Constant.KEY_GUIDE_ACTIVITY, "");//取得所有类名 如 com.my.MainActivity
    if(mResultStr.equalsIgnoreCase("false"))
        return false;
    else
        return true;
}
public class Constant {
    /**
     * 保存是否是第一次进入
     */
    public static final String SHAREDPREFERENCES_NAME = "my_pref";
    public static final String KEY_GUIDE_ACTIVITY = "guide_activity";

    public final static int SWITCH_MAINACTIVITY = 1000;
    public final static int SWITCH_GUIDACTIVITY = 1001;
}

接着主要部分来了,

handler发送消息啦,不过原本试过直接上handler,结果发现会造成内存泄露,所以使用了弱引用

private MyHandler mHandler = new MyHandler(SplashActivity.this);
private class MyHandler extends Handler{
    private final WeakReference weakReference;

    public MyHandler(SplashActivity activity){
         weakReference =  new WeakReference<>(activity);
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if(msg.what ==Constant.SWITCH_MAINACTIVITY){
            if(weakReference.get()!=null){
                weakReference.get().startActivity(new Intent(SplashActivity.this,MainActivity.class));
                finish();
            }
        }
        else{
            if(weakReference.get()!=null){

                weakReference.get().startActivity(new Intent(SplashActivity.this,GuideActivity.class));
                finish();
            }
        }
    }
}

 

你可能感兴趣的:(引导页是否是首次进入)