最佳答案
在按下后退按钮时,我希望我的应用程序进入停止状态,而不是被破坏状态。
在Android 文档中,它声明:
...并不是所有的活动都有在按下BACK时被销毁的行为。当用户开始在music应用程序中播放音乐,然后按下BACK,应用程序将覆盖正常的后退行为,防止播放器活动被破坏,并继续播放音乐,即使它的活动不再可见
如何在自己的应用程序中复制此功能?
我认为有三种可能……
@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.
Capture the back button press, then start an Activity of the home screen, effectively putting my application's Activity into the stopped state.
Edit: 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.