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

时间:2023-01-12 21:13:20

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.

它应该从内存中清除还是它的工作原理?据我所知,活动的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上的所有内容。

7 个解决方案

#1


32  

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会保持流程,这会使启动阶段更快。该过程将不会执行任何操作,如果需要回收内存,则该过程将被终止。别担心:)

#2


23  

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()后永远不会崩溃或重新启动

#3


10  

Try using

System.exit(0);

#4


6  

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

有关详细信息,请参阅Process Lifecycle文档: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.

无论如何,如果您的活动重新启动,它将必须再次完成整个启动序列,从onCreate()开始。不要认为可以隐式重用任何东西。

#5


4  

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();

#6


1  

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

#7


1  

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.

根据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工具包工程师),你的过程只会无所事事,所以没什么好担心的。

#1


32  

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会保持流程,这会使启动阶段更快。该过程将不会执行任何操作,如果需要回收内存,则该过程将被终止。别担心:)

#2


23  

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()后永远不会崩溃或重新启动

#3


10  

Try using

System.exit(0);

#4


6  

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

有关详细信息,请参阅Process Lifecycle文档: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.

无论如何,如果您的活动重新启动,它将必须再次完成整个启动序列,从onCreate()开始。不要认为可以隐式重用任何东西。

#5


4  

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();

#6


1  

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

#7


1  

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.

根据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工具包工程师),你的过程只会无所事事,所以没什么好担心的。