您的位置:首页 > 移动开发 > Android开发

Android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别

2015-07-01 13:42 507 查看
Activity类中的finish()、onDestory()和System.exit(0) 三者的区别:

finish是Activity的类,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理;当调用System.exit(0)时,杀死了整个进程,这时候活动所占的资源也会被释放。

 

Activity.finish()

Call this when your activity is done and should be closed. 

在你的activity动作完成的时候,或者Activity需要关闭的时候,调用此方法。

当你调用此方法的时候,系统只是将最上面的Activity移出了栈,并没有及时的调用onDestory()方法,其占用的资源也没有被及时释放。因为移出了栈,所以当你点击手机上面的“back”按键的时候,也不会找到这个Activity。

Activity.onDestory()

the system is temporarily destroying this instance of the activity to save space.

系统销毁了这个Activity的实例在内存中占据的空间。

在Activity的生命周期中,onDestory()方法是他生命的最后一步,资源空间什么的都没有咯~~。当重新进入此Activity的时候,必须重新创建,执行onCreate()方法。

System.exit(0)

这玩意是退出整个应用程序的,是针对整个Application的。将整个进程直接KO掉。

使用时,可以写在onDestory()方法内,亦可直接在想退出的地方直接调用:

如:System.exit(0); 或 android.os.Process.killProcess(android.os.Process.myPid());

 

文档介绍:

this.finish() in Activity only stop and destroy this activity, application still staying background (check by hold HOME button) 

I used: 

System.exit(0); 

but it still appear there! 

That's how Android works. The user/developer is not given any way to actually exit the application. When you call 'finish', the application stack is just pushed to the background. It still exists in the memory. Android itself decides when to close the application(i.e.
remove its instance from the memory) and generally this is done when your application becomes the oldest application which was not used for the longest time.

 

 

贴一张Activity生命周期图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: