覆盖后退按钮以充当主页按钮
按下后退按钮后,我希望我的应用程序进入停止状态,而不是销毁状态。
在 Android docs 中,它指出:
...并非所有活动都有按下 BACK 时被销毁的行为。当用户开始在音乐应用程序中播放音乐然后按“返回”时,应用程序将覆盖正常的返回行为,防止播放器 Activity 被破坏,并继续播放音乐,即使其 Activity 不再可见
我该如何做在我自己的应用程序中复制此功能?
我认为必须存在三种可能性...
捕获后退按钮按下(如下所示),然后调用主页按钮调用的任何方法。
<前><代码>@Override 公共布尔onKeyDown(int keyCode,KeyEvent事件){ if ((keyCode == KeyEvent.KEYCODE_BACK)) { Log.d(this.getClass().getName(), "后退按钮按下"); } 返回 super.onKeyDown(keyCode, 事件); }捕获后退按钮按下操作,然后欺骗主页按钮按下操作。
捕获后退按钮按下,然后启动主屏幕的 Activity,有效地将我的应用程序的 Activity 置于停止状态。
捕获后退按钮按下,然后启动主屏幕的 Activity,有效
编辑: 我了解服务,并且正在与此问题相关的应用程序中使用一项服务。这个问题具体是关于在按下后退按钮时将 Activity 置于停止状态而不是销毁状态。
On pressing the back button, I'd like my application to go into the stopped state, rather than the destroyed state.
In the Android docs it states:
...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
How do I replicate this functionality in my own application?
I think there must be three possibilities...
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
大多数时候,您需要创建一个 Service 来在后台执行某些操作,您可见的
Activity
只需控制此Service
。 (我确信音乐播放器的工作方式相同,因此文档中的示例似乎有点误导。)如果是这种情况,那么您的Activity
可以finish
像往常一样,Service
仍将运行。一种更简单的方法是捕获
Back
按钮按下并调用 moveTaskToBack(true) 如下:我认为首选选项应该是 Activity 正常完成并能够重新创建自身,例如,如果需要,从 Service 读取当前状态。但有时可以使用
moveTaskToBack
作为快速替代方案。注意:正如 Dave 所指出的,Android 2.0 引入了新的
onBackPressed
方法,并且 这些关于如何处理“后退”按钮的建议。Most of the time you need to create a Service to perform something in the background, and your visible
Activity
simply controls thisService
. (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 yourActivity
canfinish
as usual and theService
will still be running.A simpler approach is to capture the
Back
button press and call moveTaskToBack(true) as follows: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.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.使用以下代码:
Use the following code:
如果您想抓住后退按钮,请查看 Android 开发者博客上的这篇文章。它涵盖了在 Android 2.0 中执行此操作的更简单方法以及在 1.x 和 2.0 上运行的应用程序执行此操作的最佳方法。
但是,如果您的活动已停止,它仍然可能被终止,具体取决于设备上的内存可用性。如果您希望进程在没有 UI 的情况下运行,您应该创建一个
Service
。 文档对服务做了以下说明:这些似乎适合您的要求。
If you want to catch the Back Button have a look at this post on the Android Developer Blog. It covers the easier way to do this in Android 2.0 and the best way to do this for an application that runs on 1.x and 2.0.
However, if your Activity is Stopped it still may be killed depending on memory availability on the device. If you want a process to run with no UI you should create a
Service
. The documentation says the following about Services:These seems appropriate for your requirements.
尝试覆盖 android.app.Activity 类中定义的 void
onBackPressed()
。try to override void
onBackPressed()
defined in android.app.Activity class.如果它对其他人有帮助,我有一个具有 2 个布局的活动,我打开和关闭这些布局以提高可见性,尝试模拟一种 page1 > >第2页结构。如果他们在第 2 页上并按下后退按钮,我希望他们返回到第 1 页,如果他们按下第 1 页上的后退按钮,它应该仍然可以正常工作。它非常基本,但它有效
希望它能帮助别人,
干杯
if it helps someone else, I had an activity with 2 layouts that I toggled on and off for visibilty, trying to emulate a kind of page1 > page2 structure. if they were on page 2 and pressed the back button I wanted them to go back to page 1, if they pressed the back button on page 1 it should still work as normal. Its pretty basic but it works
hope it helps someone,
cheers
工作示例..
确保不要调用 super.onBackPressed();
这样你的后退按钮就像主页按钮一样。它不会完成您的活动,而是将其带到后台
第二种方法是在
onBackPressed
中调用moveTaskToBack(true);
并确保删除super.onBackPressed< /代码>
Working example..
Make sure don't call super.onBackPressed();
In this way your Back Button act like Home button . It doesn't finishes your activity but take it to background
Second way is to call
moveTaskToBack(true);
inonBackPressed
and be sure to removesuper.onBackPressed
更好的是,OnPause()怎么样:
当活动进入后台但尚未被终止时,作为活动生命周期的一部分调用。与 onResume() 相对应。
当 Activity B 在 Activity A 之前启动时,将在 A 上调用此回调。直到 A 的 onPause() 返回后才会创建 B,因此请务必
在此处输入代码
,不要在此处执行任何冗长的操作。此回调主要用于保存活动正在编辑的任何持久状态,并确保如果没有足够的资源来启动新活动而不先杀死该活动,则不会丢失任何内容。
这也是执行诸如停止动画和其他消耗大量 CPU 的操作的好地方,以便尽快切换到下一个活动,或者关闭独占访问的资源(例如相机)。
Even better, how about OnPause():
Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume().
When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to
enter code here
not do anything lengthy here.This callback is mostly used for saving any persistent state the activity is editing and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one.
This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.
android 2.0之后重写onBackPressed()。
例如
Override onBackPressed() after android 2.0.
Such as
我已经使用 @Mirko N. answser 使用新的自定义 EditText
然后从您的活动中设置数据
谢谢。
I have use @Mirko N. answser using made the new Custom EditText
Then set data from your activity
Thank you.
我已经尝试了上述所有解决方案,但没有一个对我有用。当尝试以调用 onCreate 的方式返回 MainActivity 时,以下代码对我有帮助:
Intent.FLAG_ACTIVITY_CLEAR_TOP 是关键。
I've tried all the above solutions, but none of them worked for me. The following code helped me, when trying to return to MainActivity in a way that onCreate gets called:
Intent.FLAG_ACTIVITY_CLEAR_TOP is the key.