在 Android 上关闭应用程序并启动主屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2042222/
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
Close application and launch home screen on Android
提问by Arutha
I have two different activities. The first launches the second one. In the second activity, I call System.exit(0)
in order to force the application to close, but the first activity is automatically displayed instead of the application returning to the home screen. How can I avoid this, and get the application to return to the home screen?
我有两种不同的活动。第一个启动第二个。在第二个活动中,我调用System.exit(0)
以强制关闭应用程序,但第一个活动会自动显示,而不是应用程序返回主屏幕。如何避免这种情况,并使应用程序返回主屏幕?
采纳答案by Romain Guy
You should really think about not exiting the application. This is not how Android apps usually work.
您真的应该考虑不退出应用程序。这不是 Android 应用程序通常的工作方式。
回答by Dave Webb
Short answer:call moveTaskToBack(true)
on your Activity
instead of System.exit()
. This will hide your application until the user wants to use it again.
简短回答:调用moveTaskToBack(true)
您的Activity
而不是System.exit()
. 这将隐藏您的应用程序,直到用户想要再次使用它。
The longer answer starts with another question: why do you want to kill your application?
更长的答案从另一个问题开始:你为什么要终止你的应用程序?
The Android OS handles memory management and processes and so on so my advice is just let Android worry about this for you. If the user wants to leave your application they can press the Home button and your application will effectively disappear. If the phone needs more memory later the OS will terminate your application then.
Android 操作系统处理内存管理和进程等,所以我的建议是让 Android 为您担心。如果用户想离开您的应用程序,他们可以按主页按钮,您的应用程序将有效消失。如果手机稍后需要更多内存,操作系统将终止您的应用程序。
As long as you're responding to lifecycle events appropriately, neither you nor the user needs to care if your application is still running or not.
只要您适当地响应生命周期事件,您和用户都不需要关心您的应用程序是否仍在运行。
So if you want to hide your application call moveTaskToBack()
and let Android decide when to kill it.
因此,如果您想隐藏您的应用程序调用moveTaskToBack()
并让 Android 决定何时终止它。
回答by jordi
The easiest way for achieving this is given below (without affecting Android's native memory management. There is no process killing involved).
下面给出了实现这一点的最简单方法(不影响 Android 的本机内存管理。不涉及进程终止)。
Launch an activity using this Intent:
Intent intent = new Intent(this, FinActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish();
In the target activity
FinActivity.class
, call finish() inonCreate
.
使用此 Intent 启动 Activity:
Intent intent = new Intent(this, FinActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish();
在目标活动中
FinActivity.class
,在 中调用finish()onCreate
。
Steps Explained:
步骤说明:
You create an intent that erases all other activities
(FLAG_ACTIVITY_CLEAR_TOP)
and delete the current activity.The activity destroys itself. An alternative is that you can make an splash screen in finActivity. This is optional.
您创建了一个删除所有其他活动
(FLAG_ACTIVITY_CLEAR_TOP)
并删除当前活动的意图。活动自毁。另一种方法是您可以在 finActivity 中制作启动画面。这是可选的。
回答by Danny Remington - OMS
Android has a mechanism in place to close an application safely per its documentation. In the last Activity that is exited (usually the main Activity that first came up when the application started) just place a couple of lines in the onDestroy() method. The call to System.runFinalizersOnExit(true)ensures that all objects will be finalized and garbage collected when the the application exits. For example:
Android 有一种机制可以根据其文档安全地关闭应用程序。在退出的最后一个活动(通常是应用程序启动时首先出现的主活动)中,只需在 onDestroy() 方法中放置几行。对System.runFinalizersOnExit(true)的调用确保在应用程序退出时所有对象都将被终结并被垃圾回收。例如:
public void onDestroy() {
super.onDestroy();
/*
* Notify the system to finalize and collect all objects of the
* application on exit so that the process running the application can
* be killed by the system without causing issues. NOTE: If this is set
* to true then the process will not be killed until all of its threads
* have closed.
*/
System.runFinalizersOnExit(true);
/*
* Force the system to close the application down completely instead of
* retaining it in the background. The process that runs the application
* will be killed. The application will be completely created as a new
* application in a new process if the user starts the application
* again.
*/
System.exit(0);
}
Finally Android will not notify an application of the HOMEkey event, so you cannot close the application when the HOMEkey is pressed. Android reserves the HOMEkey event to itself so that a developer cannot prevent users from leaving their application.
最后,Android 不会将HOME键事件通知应用程序,因此您无法在按下HOME键时关闭应用程序。Android 将HOME键事件保留 给自己,因此开发人员无法阻止用户离开他们的应用程序。
回答by amsurana
I use this method to close the Activities!
我用这个方法来关闭Activity!
public static void closeAllBelowActivities(Activity current) {
boolean flag = true;
Activity below = current.getParent();
if (below == null)
return;
System.out.println("Below Parent: " + below.getClass());
while (flag) {
Activity temp = below;
try {
below = temp.getParent();
temp.finish();
} catch (Exception e) {
flag = false;
}
}
}
回答by Samuh
You can also specify noHistory= "true" in the tag for first activity or finish the first activity as soon as you start the second one(as David said).
您还可以在第一个活动的标签中指定noHistory= "true" 或在您开始第二个活动后立即完成第一个活动(正如大卫所说)。
AFAIK, "force close" kills the process which hosts the JVM in which your application runs and System.exit() terminates the JVM running your application instance. Both are form of abrupt terminations and not advisable for normal application flow.
AFAIK,“强制关闭”会终止托管您的应用程序运行所在的 JVM 的进程,而 System.exit() 会终止运行您的应用程序实例的 JVM。两者都是突然终止的形式,不建议用于正常的应用程序流程。
Just as catching exceptions to cover logic flows that a program might undertake, is not advisable.
正如捕获异常以覆盖程序可能承担的逻辑流一样,是不可取的。
回答by dxh
When you launch the second activity, finish()
the first one immediately:
当您启动第二个活动时,finish()
第一个活动会立即:
startActivity(new Intent(...));
finish();
回答by Arnab
android.os.Process.killProcess(android.os.Process.myPid());
works fine, but it is recommended to let the Android platform worry about the memory management:-)
android.os.Process.killProcess(android.os.Process.myPid());
工作正常,但建议让Android平台担心内存管理:-)
回答by Nikhil
Use the finish
method. It is the simpler and easier method.
使用finish
方法。这是更简单、更容易的方法。
this.finish();
回答by Cytown
You can not do System.exit(), it's not safe.
你不能做 System.exit(),它不安全。
You can do this one: Process.killProcess(Process.myPid());
你可以这样做: Process.killProcess(Process.myPid());