简体   繁体   English

android finish()方法不会从内存中清除应用程序

[英]android finish() method doesn't clear app from memory

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? 它应该从内存中清除还是它的android如何工作? As I understand the LifeCycle of the Activity is finished. 据我所知,活动的LifeCycle已经完成。

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上的所有内容。

Android keeps processes around in case the user wants to restart the app, this makes the startup phase faster. 如果用户想要重新启动应用程序,Android会保持流程,这会使启动阶段更快。 The process will not be doing anything and if memory needs to be reclaimed, the process will be killed. 该过程将不会执行任何操作,如果需要回收内存,则该过程将被终止。 Don't worry about it :) 别担心:)

Best way is firstly use finish() and after that use System.exit(0) to clear static variables. 最好的方法是首先使用finish() ,然后使用System.exit(0)清除静态变量。 It will give you some free space. 它会给你一些自由空间。

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. 我的应用程序中已经尝试了3年以上的问题,这是不正确的。 Never get crashed or restart after using Exit() 使用Exit()后永远不会崩溃或重新启动

Try using 尝试使用

System.exit(0); System.exit(0);

Once onDestroy() gets called, your activity is doomed. 一旦onDestroy()被调用,您的活动就注定了。 Period. 期。

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: 有关更多信息,请参阅Process Lifecycle文档:
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(). 无论如何,如果您的活动重新启动,它将必须再次完成整个启动序列,从onCreate()开始。 Do not assume that anything can implicitly be reused. 不要以为任何东西都可以被隐含地重用。

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)从活动中调用活动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();

According to this presentation from Google I/O 2008, Finish should also cause the process to be killed, but I wrote a quick application to test this and on Android 1.5 it does not. 根据这个从谷歌I / O 2008演示文稿,完成也应该引起被杀害的过程,但我写了一个快速的应用程序来测试这一点,并在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工具包工程师),你的过程无论如何都会在那里无所事事,所以没什么好担心的。

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内存管理系统的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM