[开源学习_MeiZhi]首次进入App执行某操作,第二次进入则不再执行

来源:互联网 发布:淘宝视频下载地址提取 编辑:程序博客网 时间:2024/05/22 17:10

[开源学习__MeiZhi]首次进入App执行某操作,第二次进入则不再执行

源码片段来源: drakeet的MeiZhi 项目.
https://github.com/drakeet/Meizhi

这里写图片描述
这个功能很常见. 这样将功能封装起来看起来不错, 只是有个缺点就是每次不管要不要执行, 都会创建一个Once的匿名对象,有点浪费. 但是这种封装的思想还是不错的.
使用SnackBar的好处就是可以一直停在那个状态, 等待你的操作. 这是Toast的不足之处.

调用代码如下:

 new Once(this).show("tip_guide_6", () -> {     Snackbar.make(mRecyclerView, getString(R.string.tip_guide), Snackbar.LENGTH_INDEFINITE)             .setAction(R.string.i_know, v -> {})             .show(); });

Once类代码:

/** * Created by drakeet on 8/16/15. */public class Once {    SharedPreferences mSharedPreferences;    Context mContext;    public Once(Context context) {        mSharedPreferences = context.getSharedPreferences("once", Context.MODE_PRIVATE);        mContext = context;    }    public void show(String tagKey, OnceCallback callback) {        boolean isSecondTime = mSharedPreferences.getBoolean(tagKey, false);        if (!isSecondTime) {            callback.onOnce();            SharedPreferences.Editor editor = mSharedPreferences.edit();            editor.putBoolean(tagKey, true);            editor.apply();        }    }    public void show(int tagKeyResId, OnceCallback callback) {        show(mContext.getString(tagKeyResId), callback);    }    public interface OnceCallback {        void onOnce();    }}
3 0
原创粉丝点击