覆盖后退按钮像home键 [英] Override back button to act like home button

查看:168
本文介绍了覆盖后退按钮像home键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在pressing后退按钮,我想我的应用程序进入停止状态,而不是被破坏的状态。

On pressing the back button, I'd like my application to go into the stopped state, rather than the destroyed state.

在Android的<一个href="http://developer.android.com/intl/fr/guide/practices/ui_guidelines/activity_task_design.html">docs它指出:

In the Android docs it states:

......不是所有的活动的行为,他们被摧毁时,背面则是pssed $ P $。当用户开始播放音乐,在音乐应用程序,然后返回presses,应用覆盖正常的回行为,preventing被破坏的玩家活动,并继续播放音乐,即使它的活动不再可见

...not all activities have the behavior that they are destroyed when BACK is pressed. When the user starts playing music in the Music application and then presses BACK, the application overrides the normal back behavior, preventing the player activity from being destroyed, and continues playing music, even though its activity is no longer visible

如何复制在自己的应用程序这个功能?

我觉得必须有三种可能性...

I think there must be three possibilities...

  1. 捕获后退按钮preSS(如下图),然后调用任何方法(S)home键调用。

  1. Capture the back button press (as below) and then call whatever method(s) the home button calls.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Log.d(this.getClass().getName(), "back button pressed");
    }
    return super.onKeyDown(keyCode, event);
}

  • 捕获后退按钮preSS,然后欺骗home键preSS。

  • Capture the back button press and then spoof a home button press.

    捕获后退按钮preSS,然后启动主屏幕的活动,有效地把我的应用程序的活动进入停止状态。

    Capture the back button press, then start an Activity of the home screen, effectively putting my application's Activity into the stopped state.

    编辑: 我知道服务正在使用一个应用程序到这个问题是相关的。这个问题是特别是关于把活动进入停止状态,而不是销毁状态上pressing后退按钮。

    I know about services and am using one in the application to which this problem is related. This question is specifically about putting the Activity into the stopped state rather than the destroyed state on pressing the back button.

    推荐答案

    大多数时候,你需要创建一个的Service 执行在后台的东西,和你看到活动简单地控制这个服务。 (我敢肯定,音乐播放器的工作原理相同的方式,所以在文档中的例子似乎有点误导。)如果是这样的话,那么你的活动完成像往常一样和服务仍将运行。

    Most of the time you need to create a Service to perform something in the background, and your visible Activity simply controls this Service. (I'm sure the Music player works in the same way, so the example in the docs seems a bit misleading.) If that's the case, then your Activity can finish as usual and the Service will still be running.

    有一个简单的方法是捕捉返回按钮preSS并调用<一href="http://developer.android.com/intl/fr/reference/android/app/Activity.html#moveTaskToBack%28boolean%29">moveTaskToBack(true)如下:

    A simpler approach is to capture the Back button press and call moveTaskToBack(true) as follows:

    // 2.0 and above
    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }
    
    // Before 2.0
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    

    我觉得pferred选项$ P $应该是一个活动正常结束,并能够重新本身,例如如果需要读取从一个服务的当前状态。但 moveTaskToBack 可以用作偶尔的快速替代。

    I think the preferred option should be for an Activity to finish normally and be able to recreate itself e.g. reading the current state from a Service if needed. But moveTaskToBack can be used as a quick alternative on occasion.

    注意:正如戴维以下的Andr​​oid 2.0引入了一个新的 onBack pressed 方法,和<一href="http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html">these建议的关于如何处理Back按钮。

    NOTE: as pointed out by Dave below Android 2.0 introduced a new onBackPressed method, and these recommendations on how to handle the Back button.

    这篇关于覆盖后退按钮像home键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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