Android 生命周期

Activity生命周期包含7种状态

1  onCreate   Called when the activity is first created. 当 activity第一次被创建时被调用

2  onStart   Called when the activity is becoming visible to the user. 当activity被用户看到时调用

3 onResume  Called when the activity will start interacting with the user.当用户可以对界面进行操作时调用 

4 onPause  Called when the system is about to start resuming a previous activity  当用户将要离开当前页面时调用

5 onRestart   Called after your activity has been stopped, prior to it being started again当activity被停止后,在被开启前被调用

6 onStop  Called when the activity is no longer visible to the user  当用户看不到此activity界面时调用

7 onDestroy

 其生命周期以activity栈的方式进行管理, 当app启动时系统默认会加载配置好的第一个activity,这时会调用onCreate 、 onStart、onResume方法,同时第一个activity入栈,假设第一个activity界面包含一个按钮,点击调用第二个activity,那么会调用第一个activity的onPause、onStop方法,同时调用第二个activity的onCreate 、 onStart、onResume方法,第二个 activity 入栈;也就是说栈顶元素一定是用户看到的activity。当用户点击返回时,第二个activity会调用onPause、onStop、onDestroy,同时第一个activity调用onRestart、onStart、onResume,第二个activity出栈。

问题1  : 什么时候会调用ondestroy方法

    当用户退出程序、系统资源不充足、用户手动调用finish方法

问题 2: 当用户正在操作某个app,突然有电话打进来时,会调用那些方法?

    首先会调用onPause 、onStop  保存用户操作的状态、数据,通话结束后会恢复通话之前的状态

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章