先上经典的Activity生命周期图
Case 1: A Activity 跳转到 B Activity
ActivityStack: FirstActivity onPause
ActivityStack: SecondActivity onCreate
ActivityStack: SecondActivity onStart
ActivityStack: SecondActivity onResume
ActivityStack: FirstActivity onSaveInstanceState
ActivityStack: FirstActivity onStop
从A Activity 跳转到 B Activity,是先调用A Activity的onPause,然后走完B Activity的生命周期,最后才走A Activity的 onStop
Case 2: A B C 顺序的Activity,C Activity 后台被杀
// SecondActivity 启动 ThirdActivity
ActivityStack: SecondActivity onPause
ActivityStack: ThirdActivity onCreate
ActivityStack: ThirdActivity onStart
ActivityStack: ThirdActivity onResume
ActivityStack: SecondActivity onSaveInstanceState
ActivityStack: SecondActivity onStop
// 应用退到后台
ActivityStack: ThirdActivity onPause
ActivityStack: ThirdActivity onSaveInstanceState
ActivityStack: ThirdActivity onStop
// 杀进程,重新启动APP
ActivityStack: ThirdActivity onCreate
ActivityStack: ThirdActivity onStart
ActivityStack: ThirdActivity onRestoreInstanceState
ActivityStack: ThirdActivity onResume
// 按返回键。创建SecondActivity,并调用SecondActivity的onRestoreInstanceState
ActivityStack: ThirdActivity onPause
ActivityStack: SecondActivity onCreate
ActivityStack: SecondActivity onStart
ActivityStack: SecondActivity onRestoreInstanceState
ActivityStack: SecondActivity onResume
ActivityStack: ThirdActivity onStop
ActivityStack: ThirdActivity onDestroy
ThirdActivity后台时,APP被杀。由于已经调用了ThirdActivity的onSaveInstanceState,所以重启APP,恢复的是ThirdActivity
多Activity时应用被杀,重启APP不会把多Activity都创建,只会创建栈顶的Activity,但是Activity栈记录还会保留,按Back键依然会返回并创建SecondActivity
Case 3: A B C 顺序的Activity,C Activity 前台被杀
// 从SecondActivity跳转到ThirdActivity
ActivityStack: SecondActivity onPause
ActivityStack: ThirdActivity onCreate
ActivityStack: ThirdActivity onStart
ActivityStack: ThirdActivity onResume
ActivityStack: SecondActivity onSaveInstanceState
ActivityStack: SecondActivity onStop
// ThirdActivity前台时,进程被杀
ActivityStack: SecondActivity onCreate
ActivityStack: SecondActivity onStart
ActivityStack: SecondActivity onRestoreInstanceState
ActivityStack: SecondActivity onResume
由于ThirdActivity没有来得及调用onSaveInstanceState,所以恢复的是SecondActivity
出现情景:ThirdActivity 位于前台,不可能由于内存不足而被系统回收(内存不足,最多就抛OOM)。只可能出现Crash或被命令行强制kill。
Case 4:onNewIntent()
SingleTop/SingleTask的Activity,自己启动自己的时候
onPause
onNewIntent
onResume
从别的任务栈start栈顶的SingleTop/SingleTask的Activity
ActivityStack: ThirdActivity onPause
ActivityStack: SecondActivity onNewIntent
ActivityStack: SecondActivity onRestart
ActivityStack: SecondActivity onStart
ActivityStack: SecondActivity onResume
ActivityStack: ThirdActivity onSaveInstanceState
ActivityStack: ThirdActivity onStop
Case 5:不保留活动
// Activity 退到后台
ActivityStack: FirstActivity onPause
ActivityStack: FirstActivity onSaveInstanceState
ActivityStack: FirstActivity onStop
ActivityStack: FirstActivity onDestroy
// Activity回到前台
ActivityStack: FirstActivity onCreate
ActivityStack: FirstActivity onStart
ActivityStack: FirstActivity onRestoreInstanceState
ActivityStack: FirstActivity onResume
Activity恢复原则
-
单Activity恢复
恢复该Activity -
多Activity恢复
只会恢复栈顶的activity,其他Activity不会创建。但是activity record里是有记录其他Activity。假如按下返回键,就会创建其他Activity
注意点
- onSaveInstanceState :调用位于 onPause 到 onStop之间
- onRestoreInstanceState :Activity被系统销毁了(后台回收进程/设置不保留活动模式时退后台时),当Activity恢复,调用位于 onStart 到 onResume之间.
- onSaveInstanceState方法和onRestoreInstanceState方法“不一定”是成对的被调用的
- onRestart: 调用位于onStop到onStart之间
应用被杀
系统回收 / 崩溃 / 强制杀进程 ,都不会回调系统Activity生命周期的函数
网友评论