单击后退按钮两次以退出带有 rxjava 的活动 [英] Clicking back button twice to exit an activity with rxjava

查看:67
本文介绍了单击后退按钮两次以退出带有 rxjava 的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一种微妙的 rx 方法来退出活动,同时按两次后退按钮.

Looking for a subtle rx approach to exit an activity while pressing back button two times.

boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;                       
        }
    }, 2000);
} 

推荐答案

我建议采用稍微不同的方法.实际上,我们正在寻找的是两次点击之间的时间.RxJava 有运算符 interval(TimeUnit),这正是我们想要的.所以,我们有我们的主题和期望的退出超时.

I would suggest slightly different approach. Actually, what we are looking for, is time between 2 clicks. RxJava has operator interval(TimeUnit), which gives us exactly what we want. So, we have our subject and desired exit timeout.

private static final long EXIT_TIMEOUT = 2000;
private CompositeDisposable compositeDisposable = new CompositeDisposable();
private PublishSubject<Boolean> backButtonClickSource = PublishSubject.create();

在 onResume 中,我们向主题添加运算符并订阅它.结果我们将添加到 CompositeDisposable,以便稍后能够与您可能在您的活动中拥有的所有其他订阅一起处理它.

In onResume we add operators to our subject and subscribe to it. Result we are adding to CompositeDisposable, to be able to dispose it later with all other subscriptions you probably have in your activity.

@Override
protected void onResume() {
    super.onResume();

    compositeDisposable.add(backButtonClickSource
            .debounce(100, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext(new Consumer<Boolean>() {
                @Override
                public void accept(@NonNull Boolean event) throws Exception {
                    Toast.makeText(MainActivity.this, "Please press back once more to exit", Toast.LENGTH_SHORT).show();
                }
            })
            .timeInterval(TimeUnit.MILLISECONDS)
            .skip(1)
            .filter(new Predicate<Timed<Boolean>>() {
                @Override
                public boolean test(@NonNull Timed<Boolean> interval) throws Exception {
                    return interval.time() < EXIT_TIMEOUT;
                }
            })
            .subscribe(new Consumer<Timed<Boolean>>() {
                @Override
                public void accept(@NonNull Timed<Boolean> interval) throws Exception {
                    finish();
                }
            }));
}

我们使用 debounce 来消除噪音(可能是不必要的).然后在每次点击时我们向用户显示消息(无论你想要什么,为了简单起见,我使用了 Toast).在我们切换到主线程之前,否则会抛出异常.我们跳过第一个事件,否则订阅和第一次点击之间的时间间隔将被发出,如果它足够小,只需一次点击就会退出.我们过滤掉所有大于 EXIT_TIMEOUT 的间隔.最后,当我们得到足够小的时间间隔时,这不是第一次,我们完成了我们的活动.

We use debounce to get rid of noise (maybe unnecessary). Then on each click we show message to user (whatever you want, for sake of simplicity I used Toast). Before we switch to main thread, otherwise exception will be thrown. First event we skip, because otherwise time interval between subscribe and first click will be emitted, and if it's small enough, exit will happen after just one click. All the intervals that are bigger than our EXIT_TIMEOUT we filter out. And finally, when we get small enough time interval, which is not first, we finish our activity.

然后,在 onPause 中,我们应该清除 CompositeDisposable 以便不再获得点击事件.

Then, in onPause we should clear our CompositeDisposable in order not to get click events anymore.

@Override
protected void onPause() {
    super.onPause();

    compositeDisposable.clear();
}

当然,在 onBackPressed() 中,我们应该将返回按钮的点击转发到我们的 PublishSubject.

And of course in onBackPressed() we should forward back button clicks to our PublishSubject.

@Override
public void onBackPressed() {
    backButtonClickSource.onNext(true);
}

这篇关于单击后退按钮两次以退出带有 rxjava 的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆