龙须面

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

上图就是android应用程序的一个生命周期,需要明确的一点是,在stoped阶段,没有任何代码执行!

通常情况下,不需要实现destroyed的方法来清理系统的垃圾以及其它操作,但是在下列情况时,要实现它:

  activity includes background threads that you created during onCreate() or other other long-running resources that could potentially leak   memory if not properly closed, you should kill them during onDestroy(). 

例如:

1 @Override
2 public void onDestroy() {
3     super.onDestroy();  // Always call the superclass
4     
5     // Stop method tracing that the activity started during onCreate()
6     android.os.Debug.stopMethodTracing();
7 }

注意:

he system calls onDestroy() after it has already called onPause() and onStop() in all situations except one: when you call finish() from within the onCreate() method. In some cases, such as when your activity operates as a temporary decision maker to launch another activity, you might callfinish() from within onCreate() to destroy the activity. In this case, the system immediately callsonDestroy() without calling any of the other lifecycle methods.

posted on 2012-07-04 13:51  木子小黑  阅读(150)  评论(0编辑  收藏  举报