美文网首页
快速点击多次弹出相同Activity

快速点击多次弹出相同Activity

作者: 微笑中的你 | 来源:发表于2021-06-25 09:30 被阅读0次

    方法一 singleTop

    清单文件或者Intent中标记

            <activity
                android:name=".activity.GwzDeviceInfoActivity"
                android:launchMode="singleTop"
                android:screenOrientation="portrait"></activity>
    
    
    
                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

          本文链接:https://www.haomeiwen.com/subject/qvmnyltx.html