android finish() 方法不会从内存中清除应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1977246/
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
android finish() method doesn't clear app from memory
提问by Daniel Benedykt
I have an activity and I call the finish() method and the activity is not cleared from memory.
我有一个活动,我调用了 finish() 方法,但该活动并未从内存中清除。
After calling finish() , I see that the method onDestroy() is executed successfully (and I clear all my variables and stuff in there).
在调用 finish() 之后,我看到方法 onDestroy() 已成功执行(并且我清除了那里的所有变量和内容)。
Should it be cleared from memory or its how android works? As I understand the LifeCycle of the Activity is finished.
它应该从内存中清除还是它的 android 工作原理?据我了解,活动的生命周期已完成。
And if it keeps the app in memory so it runs faster the 2nd time the user uses it, what kind of objects can I leave in memory to reuse? If I understand correctly, I am suppose to clear everything on onDestroy.
如果它将应用程序保存在内存中,以便在用户第二次使用它时运行得更快,那么我可以将哪些对象留在内存中以供重用?如果我理解正确,我应该清除 onDestroy 上的所有内容。
回答by Romain Guy
Android keeps processes around in case the user wants to restart the app, this makes the startup phase faster. The process will not be doing anything and if memory needs to be reclaimed, the process will be killed. Don't worry about it :)
如果用户想要重新启动应用程序,Android 会保留进程,这使得启动阶段更快。该进程不会做任何事情,如果需要回收内存,该进程将被杀死。别担心 :)
回答by Tefel
Best way is firstly use finish()
and after that use System.exit(0)
to clear static variables. It will give you some free space.
最好的方法是先使用finish()
,然后使用System.exit(0)
清除静态变量。它会给你一些自由空间。
A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory
许多应用程序留下了让我生气的工作流程和变量。使用 30 分钟后内存已满,我必须运行任务管理器 - Lvl 2 清除内存
Its not true that is cousing problems i've tried it for over 3 years in my apps. Never get crashed or restart after using Exit()
我已经在我的应用程序中尝试了 3 年多,这不是真实的问题。使用后永不死机或重启Exit()
回答by Zahid
Try using
尝试使用
System.exit(0);
System.exit(0);
回答by Trevor Johns
Once onDestroy() gets called, your activity is doomed. Period.
一旦 onDestroy() 被调用,你的活动就注定失败。时期。
That being said, the process (and hence address space) allocated to your application might still be in use by another part of your application -- another activity or service. It's also possible that your process is empty and the OS just hasn't gotten around to reclaiming it yet; it's not instant.
话虽如此,分配给应用程序的进程(以及地址空间)可能仍在被应用程序的另一部分使用——另一个活动或服务。也有可能你的进程是空的,操作系统还没有来回收它;这不是即时的。
See the Process Lifecycle document for more information:
http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle
有关更多信息,请参阅进程生命周期文档:http:
//developer.android.com/reference/android/app/Activity.html#ProcessLifecycle
Regardless, if your activity is relaunched, it will have to go through the entire startup sequence again, starting with onCreate(). Do not assume that anything can implicitly be reused.
无论如何,如果您的 Activity 重新启动,它将必须再次执行整个启动序列,从 onCreate() 开始。不要假设任何东西都可以隐式重用。
回答by Anton Derevyanko
If you need to close application from subactivity, I may suggest you to made it in such a way: 1) from activity A call activity B as startActivityForResult(intent, REQUEST_CODE);
如果您需要从子活动关闭应用程序,我建议您以这种方式进行: 1) 从活动 A 调用活动 B 作为 startActivityForResult(intent, REQUEST_CODE);
Intent intent = new Intent()
.setClass(ActivityA.this, ActivityB.class);
startActivityForResult(intent, REQUEST_CODE);
2) in activity A you should add method:
2)在活动A中,您应该添加方法:
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_CLOSE_APPLICATION) {
this.finish();
}
}
}
3) in activity B call finish:
3)在活动B调用完成:
this.setResult(RESULT_CLOSE_APPLICATION);
this.finish();
回答by Rab Ross
As a quick fix you can use the folowing to kill your app:
作为快速修复,您可以使用以下内容来终止您的应用程序:
android.os.Process.killProcess(android.os.Process.myPid());
But I would not recommend this for commercial apps because it goes against how the Android memory management system works.
但我不建议将其用于商业应用程序,因为它与 Android 内存管理系统的工作方式背道而驰。
回答by Frank
According to thispresentation from Google I/O 2008, Finish shouldalso cause the process to be killed, but I wrote a quick application to test this and on Android 1.5 it does not.
根据Google I/O 2008 的这个演示,Finish也应该导致进程被终止,但我写了一个快速应用程序来测试这个,而在 Android 1.5 上它没有。
As Romain said (who incidentally is a UI Toolkit engineer for Android), your process will just sit there doing nothing anyways, so it is nothing to worry about.
正如 Romain 所说(顺便说一句,他是 Android 的 UI 工具包工程师),您的进程将只是坐在那里什么也不做,所以没有什么可担心的。