覆盖后退按钮以充当主页按钮 [英] Override back button to act like home button

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

问题描述

按下后退按钮时,我希望我的应用程序进入停止状态,而不是销毁状态.

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

在 Android docs 中,它指出:

In the Android docs it states:

...并非所有活动都有在按下 BACK 时被销毁的行为.当用户在 Music 应用程序中开始播放音乐然后按 BACK 时,应用程序会覆盖正常的返回行为,防止播放器 Activity 被破坏,并继续播放音乐,即使其 Activity 不再可见

...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. 捕获后退按钮按下(如下所示),然后调用主页按钮调用的任何方法.

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

  • 捕获后退按钮的按下,然后欺骗主页按钮的按下.

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

    捕获后退按钮按下,然后启动主屏幕的 Activity,有效地将我的应用程序的 Activity 置于停止状态.

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

    我了解服务,并且正在与此问题相关的应用程序中使用服务.这个问题具体是关于在按下后退按钮时将 Activity 置于停止状态而不是销毁状态.

    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 在后台执行某些操作,您的可见 Activity 只需控制此 Service.(我确定音乐播放器的工作方式相同,因此文档中的示例似乎有点误导.)如果是这种情况,那么您的 Activity 可以 finish像往常一样,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.

    一种更简单的方法是捕获 Back 按钮按下并调用 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);
    }
    

    我认为首选的选项应该是让 Activity 正常完成并能够重新创建自己,例如如果需要,从服务读取当前状态.但是 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.

    注意:正如 Dave 在 Android 2.0 下面所指出的,引入了一个新的 onBackPressed 方法,以及 这些建议关于如何处理后退按钮.

    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.

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

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