快速点击多次弹出相同Activity

方法一 singleTop

清单文件或者Intent中标记

        



            Intent intent = new Intent(this, GwzDeviceInfoActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivityForResult(intent, 102);

实践检测: NO


方法二 记录点击时间点,短期内点击不执行

public abstract class SingleClickListener implements View.OnClickListener {

    //间隔时间
    private int internal = 1000;

    private long lastClickTime;


    public SingleClickListener(){
        super();
    }

    public SingleClickListener(int internal) {
        this();
        this.internal = internal;
    }


    public abstract void onSingleClick(View view);


    @Override
    public void onClick(View v) {
        //在间隔时间 外 才执行
        if (!isQuicklyClick()) {
            onSingleClick(v);
        }
    }


    /**
     * 是在 间隔内
     * @return boolean
     */
    private boolean isQuicklyClick() {
        long cur = System.currentTimeMillis();
        boolean s = cur - lastClickTime < internal;
        lastClickTime = cur;
        return s;
    }
}

实践检测: NO


方法三 判断栈顶Activity是不是要弹出的Activity

要在Application中监听周期方法


    private Activity showActivity;
    void initGlobeActivity() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                Log.d(TAG, "--------------------onActivityCreated: " + activity.getClass().getName());
                showActivity = activity;

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                Log.d(TAG, "--------------------onActivityStarted: " + activity.getClass().getName());
                showActivity = activity;
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                Log.d(TAG, "--------------------onActivityResumed: " + activity.getClass().getName());
                showActivity = activity;

            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
                Log.d(TAG, "---------------------onActivityPaused: " + activity.getClass().getName());

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                Log.d(TAG, "--------------------onActivityStopped: " + activity.getClass().getName());

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
                Log.d(TAG, "-------------------onActivitySaveInstanceState: " + activity.getClass().getName());

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                Log.d(TAG, "-------------------onActivityDestroyed: " + activity.getClass().getName());
            }
        });
    }
    public Activity getCurrentActivity() {
        return showActivity;
    }

在弹出Activity的地方调用

        Activity currentActivity = LzApplication.getInstance().getCurrentActivity();
        if (currentActivity instanceof  GwzDeviceInfoActivity) {

        } else {
            Intent intent = new Intent(this, GwzDeviceInfoActivity.class);
            startActivityForResult(intent, 102);
        }

实践检测:YES


你可能感兴趣的:(快速点击多次弹出相同Activity)