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.
After calling finish() , I see that the method onDestroy() is executed successfully (and I clear all my variables and stuff in there).
Should it be cleared from memory or its how android works? As I understand the LifeCycle of the Activi开发者_JS百科ty is finished.
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.
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 :)
Best way is firstly use finish()
and after that use System.exit(0)
to clear static variables. 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
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()
Try using
System.exit(0);
Once onDestroy() gets called, your activity is doomed. 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:
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.
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);
Intent intent = new Intent()
.setClass(ActivityA.this, ActivityB.class);
startActivityForResult(intent, REQUEST_CODE);
2) in activity A you should add method:
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:
this.setResult(RESULT_CLOSE_APPLICATION);
this.finish();
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.
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.
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.
精彩评论