安卓:两次单击后退按钮退出活动 [英] Android: clicking TWICE the back button to exit activity

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

问题描述

我已经注意到这种模式在许多Android应用和游戏的最近:单击后退按钮退出的应用程序,吐司来了一个时类似的消息请单击后退再次退出。

我不知道,因为我看到它越来越多的时候,是一个内置的,你能以某种方式在活动中访问功能?我已经看了很多类的源$ C ​​$ C,但我似乎无法找到任何东西。

当然,我能想到的几种方法可以很容易地实现相同的功能(最简单的可能是保持一个布尔值的活动,指示用户是否已经点击一次...),但我不知道是否有一些已经在这里了。

修改:作为@LAS_VEGAS提到的,我并没有真正指的是在传统意义上的退出。 (即终止)我的意思是要回什么是开放之前,应用程序启动活动发起,如果是有道理的:)

解决方案

 布尔doubleBackToExit pressedOnce = FALSE;

@覆盖
公共无效onBack pressed(){
    如果(doubleBackToExit pressedOnce){
        super.onBack pressed();
        返回;
    }

    this.doubleBackToExit pressedOnce = TRUE;
    Toast.makeText(这一点,请再次点击返回退出,Toast.LENGTH_SHORT).show();

    新的处理程序()。postDelayed(新的Runnable(){

        @覆盖
        公共无效的run(){
            doubleBackToExit pressedOnce = FALSE;
        }
    },2000);
}
 

我觉得这个处理程序有助于经过2秒钟,重设变量。

I've noticed this pattern in a lot of Android apps and games recently: when clicking the back button to "exit" the application, a Toast comes up with a message similar to "Please click BACK again to exit".

I was wondering, as I'm seeing it more and more often, is that a built-in feature that you can somehow access in an activity? I've looked at the source code of many classes but I can't seem to find anything about that.

Of course, I can think about a few ways to achieve the same functionality quite easily (the easiest is probably to keep a boolean in the activity that indicates whether the user already clicked once...) but I was wondering if there's something already here.

EDIT: As @LAS_VEGAS mentioned, I didn't really mean "exit" in the traditional meaning. (i.e. terminated) I meant "going back to whatever was open before the application start activity was launched", if that makes sense :)

解决方案

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);
} 

I Think this handler helps to reset the variable after 2 second.

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

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