覆盖后退按钮像home键按钮、home

2023-09-11 10:51:23 作者:你别皱眉i

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

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:

......不是所有的活动的行为,他们被摧毁时,背面则是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...

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

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。

港行iphone不按home键怎么回到主页面

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.

推荐答案

大多数时候,你需要创建一个的