Android 按 Back 是否总是导致 Activity 完成()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2800950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does pressing Back always cause Activity to finish()?
提问by stormin986
I've heard that pressing the back button will essentially cause the current Activity to finish()
. Is this always the case? Seems like it would be with the way it pops the Activity off the stack.
我听说按下后退按钮基本上会导致当前 Activity 到finish()
. 总是这样吗?似乎它将与它从堆栈中弹出 Activity 的方式有关。
The one situation I'm not so sure about is when the root Activity in a Task has back pressed. I'm currently experiencing a very weird effect, described as follows:
我不太确定的一种情况是任务中的根活动何时被按下。我目前正在经历一个非常奇怪的效果,描述如下:
On loading my application, the first Activity is for initialization, and once it finishes, it calls my main Activity (a TabActivity). This first init activity has android:noHistory="true" set in the Manifest so pressing Back from my main Activity won't go back to that. It goes to the Launcher. When I click on my App in the Launcher a second time, the initialization activity loads again, and loads the main Activity when done. Almost immediately after, it loads a second instance of my main Activity. But ONLY after the Application has already been run once, and was exited by pressing BACK from the main Activity. It does it every subsequent time until I force quit the app or load a new version from the IDE.
在加载我的应用程序时,第一个 Activity 用于初始化,一旦它完成,它就会调用我的主 Activity(一个 TabActivity)。第一个 init Activity 在 Manifest 中设置了 android:noHistory="true" ,所以从我的主 Activity 按下 Back 不会回到那个状态。它转到启动器。当我第二次在启动器中单击我的应用程序时,初始化活动再次加载,并在完成后加载主活动。几乎立即之后,它加载了我的主要活动的第二个实例。但只有在应用程序已经运行一次,并通过从主活动按 BACK 退出之后。以后每次都会这样做,直到我强制退出应用程序或从 IDE 加载新版本。
Based on this, I am suspecting some kind of Activity instance is lying around and being reused, since it only happens on the second+ time I run the application (and exit with BACK -- using HOME just returns to the last state of the app, no big deal). Anyone have any thoughts??
基于此,我怀疑某种 Activity 实例正在闲置并被重用,因为它仅在我第二次运行应用程序时发生(并以 BACK 退出 - 使用 HOME 仅返回到应用程序的最后状态,没什么大不了)。有人有什么想法吗??
采纳答案by hara
I've heard that pressing the back button will essentially cause the current Activity to finish(). Is this always the case?
我听说按下后退按钮基本上会导致当前活动完成()。总是这样吗?
No it is not. The most activities have this behaviour but not all. For example you could create a Dialog and set it setCancelable(false)and it won't close if you click BACK button.
不它不是。大多数活动都有这种行为,但不是全部。例如,您可以创建一个 Dialog 并将其设置为setCancelable(false)并且如果您单击 BACK 按钮它不会关闭。
Furthermore you could customize activity behaviour on BACK button pressed by overriding onBackPressed
此外,您可以通过覆盖onBackPressed自定义按下后退按钮的活动行为
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
当活动检测到用户按下后退键时调用。默认实现只是完成当前活动,但您可以覆盖它以执行您想要的任何操作。
About your application behaviour..Did you verify if the activity launcher is finished after it loads your main activity? I mean if the onDestroy() method is called. Maybe after it runs the main activity it remains there and when you click back you just go back to the old Launcher...
关于您的应用程序行为..您是否在加载主要活动后验证活动启动器是否已完成?我的意思是如果 onDestroy() 方法被调用。也许在它运行主要活动后,它会留在那里,当你点击返回时,你就会回到旧的启动器......
hope this helps..
希望这可以帮助..
回答by Michael A.
Read through the Activity and Task design guidelineson the Android developer site; they explain how the Home and Back buttons work. Obviously, if you override the default behavior (as mentioned by hara above), the back button will not finish the activity.
通读Android 开发者网站上的活动和任务设计指南;他们解释了 Home 和 Back 按钮的工作原理。显然,如果您覆盖默认行为(如上文 hara 所述),则后退按钮将不会完成活动。
On your specific issue, check your logcat. You should be able to see there whether it is bringing an old process back to life or starting up a new one. If that is unclear, insert a couple of log statements into onCreate, onPause, onDestroyed, etc., so that you can see exactly what is happening with your process.
关于您的具体问题,请检查您的 logcat。您应该能够在那里看到它是让旧流程恢复生机还是启动一个新流程。如果不清楚,请在 onCreate、onPause、onDestroyed 等中插入几条日志语句,以便您可以准确了解流程中发生的情况。
回答by Smith
You can control BACK-BUTTON by writing the following code.
您可以通过编写以下代码来控制 BACK-BUTTON。
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK ) {
//preventing default implementation previous to
//android.os.Build.VERSION_CODES.ECLAIR
return false;
}
return super.onKeyDown(keyCode, event);
}
回答by Steve Haley
Are you running your activities with any special flags, such as singleInstance or singleTop? Those could be causing the oddities you're seeing. The easiest way to track down what's causing your problem is to absolutely fill it with debugging messages. For example:
您是否使用任何特殊标志运行您的活动,例如 singleInstance 或 singleTop?这些可能会导致您看到的奇怪现象。追踪导致问题的最简单方法是用调试消息完全填充它。例如:
- In your initialisation activity, add a log in the beginning of onCreate to get the name of the activity such as this.toString(). More on why you want this line later.
- When it launches the main tabbed activity, get the name of the launching activity and a message saying it's launched the tabbed one.
- Override the onPause(), onStop() and onDestroy() callbacks and add debugging lines with this.toString() and also a message telling you which callback it is.
- 在您的初始化活动中,在 onCreate 的开头添加一个日志以获取活动的名称,例如 this.toString()。更多关于为什么你稍后想要这条线。
- 当它启动主选项卡式活动时,获取启动活动的名称和一条消息,说明它已启动选项卡式活动。
- 覆盖 onPause()、onStop() 和 onDestroy() 回调并使用 this.toString() 添加调试行以及一条消息,告诉您它是哪个回调。
What this will do is tell you whether you've got multiple instances of the initialisation activity lying around. To this by comparing the name of the activities calling your main activity with the ones that were just created and the ones that went through to onDestroy.
这将告诉您是否有多个初始化活动的实例。为此,将调用您的主要活动的活动名称与刚刚创建的活动名称以及经过 onDestroy 的活动名称进行比较。
If you don't know how to debug, use Log.d(LOG_TAG, "Your message here");
. And then define a constant LOG_TAG String somewhere. After that, show the LogCat perspective in Eclispe by going to Window, show perspective (or view, don't remember exactly), other, Android, LogCat. The purpose of having a LOG_TAG constant is that you can set up LogCat to filter to that String and only show you those messages. It will make it easier to see them among the mass of system log messages.
如果您不知道如何调试,请使用Log.d(LOG_TAG, "Your message here");
. 然后在某处定义一个常量 LOG_TAG String。之后,在 Eclispe 中显示 LogCat 透视图,方法是转到 Window、显示透视图(或视图,记不清了)、其他、Android、LogCat。拥有 LOG_TAG 常量的目的是您可以设置 LogCat 以过滤到该字符串并仅向您显示这些消息。这将使在大量系统日志消息中更容易看到它们。
回答by Matt J.
The short answer to the original question is 'no'. This is largely because, unfortunately, not every developer follows the guidelines referenced by previous answers.
对原始问题的简短回答是“否”。不幸的是,这主要是因为并非每个开发人员都遵循先前答案中引用的准则。
Yet the guidleines themselves mention exceptions, when the Back key should not call finish(). the most prominent exception is the Web browser, which has its own "back stack" for each window, so it must have its own custom handling of the Back key.
然而,指南本身提到了异常,当 Back 键不应该调用 finish() 时。最突出的例外是 Web 浏览器,它对每个窗口都有自己的“后退堆栈”,因此它必须有自己对后退键的自定义处理。
回答by Jacob Tabak
If there are no fragments on the back stack and a developer has not overridden onBackPressed, the activity will finish when the back button is pressed.
如果返回堆栈上没有片段并且开发人员没有覆盖 onBackPressed,则当按下返回按钮时活动将结束。
Here is the source code for Android 4.4.2 Activity.onBackPressed():
这是 Android 4.4.2 Activity.onBackPressed() 的源代码:
public void onBackPressed() {
if (!mFragments.popBackStackImmediate()) {
finish();
}
}
回答by Nishanth S Babu
just override onbackpressed().. on back press this method will get execute remove super and do what u want to do.
只需覆盖 onbackpressed().. 回按此方法将执行 remove super 并执行您想做的事情。