Android Activity生命周期

12年前

onCreate(创建) 、onStart(激活) 、onResume(恢复) 、onPause(暂停) 、onStop(停止) 、onDestroy(销毁) 、onRestart(重启)

Activity starts -->onCreate()-->onStart()-->onResume()-->activity is running--another activity comes in front of this activity-->onPause()--the activity is no longer visivble-->onStop()-->onDestroy()-->activity is shut down

notes:

        When the “onPause()“excute,other applications need memory ,then Process is killed(User navigates back to activity),last excute onCreate()。

 When  the “onPause()“excute,the activity comes to the foreground,so it will excute onRestart(),last excute onstart()。

各个函数使用的时间:

1、onCreate()    
Acitivity首次创建时被调用。用于设置Acitivity的布局文件,绑定按钮监听器等一些普通静态操作。

2、onStart()
Acitivity对用户可见时被调用。

3、onResume()
Acitivity获得用户焦点,即用户可操作该Acitivity时被调用。

4、onPause()
应用程序启动了其它Acitivity时被调用。一般用于保存当前Acitivity中的数据。

5、onStop()
Acitivity不可见时被调用。

6、onRestart()
已停止的Acitivity重新启动时被调用。

7、onDestroy()
调用Acitivity的finish()方法或Android系统资源不足时被调用。