Activity生命周期总结

Posted bdmh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activity生命周期总结相关的知识,希望对你有一定的参考价值。

Activity生命周期总结

方式生命周期备注
单一Activity(启动)

onCreate

onStart

onResume

 
单一Activity(关闭)

onStop

onDestroy

 
MainActivity中打开TestActivity

MainActivity: onPause

TestActivity: onCreate

TestActivity: onStart

TestActivity: onResume

MainActivity: onStop

注意TestActivity的周期加在MainActivity之间,而不是连续完成每一个
上面情况中关闭TestActivity

TestActivity: onPause

MainActivity: onRestart

MainActivity: onStart

MainActivity: onResume

TestActivity: onStop

TestActivity: onDestroy

注意顺序
TestActivity中打开DialogTestActivity无影响
AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(TestActivity.this);

 

中的Dialog中打开ThirdActivity

TestActivity: onPause

ThirdActivity: onCreate

ThirdActivity: onStart

ThirdActivity: onResume

TestActivity: onStop

 
上面情况中关闭ThirdActivity

ThirdActivity: onPause

TestActivity: onRestart

TestActivity: onStart

TestActivity: onResume

ThirdActivity: onStop

ThirdActivity: onDestroy

 

TestActivity中打开ThirdActivity

ThirdActivity设置为透明

TestActivity: onPause

ThirdActivity: onCreate

ThirdActivity: onStart

ThirdActivity: onResume

因为ThirdActivity透明,TestActivity处于可见状态,所以TestActivity没有onStop和onDestroy

关闭上面情况中的ThirdActivity

ThirdActivity: onPause

TestActivity: onResume

ThirdActivity: onStop

ThirdActivity: onDestroy

原因同上

TestActivity中打开ThirdActivity

ThirdActivity设置Dialog主题

TestActivity: onPause
ThirdActivity: onCreate
ThirdActivity: onStart
ThirdActivity: onResume
 
关闭上面情况中的ThirdActivityThirdActivity: onPause
TestActivity: onResume
ThirdActivity: onStop
ThirdActivity: onDestroy
 

TestActivity横屏

由横屏切换成竖屏

TestActivity: onConfigurationChanged

TestActivity: onPause

TestActivity: onStop

TestActivity: onDestroy

TestActivity: onCreate

TestActivity: onStart

TestActivity: onRestoreInstanceState

TestActivity: onResume

不设置orientation|keyboardHidden|screenSize,设置后,不影响TestActivity的生命周期
退到后台TestActivity: onPause
TestActivity: onStop
 
返回前台TestActivity: onRestart
TestActivity: onStart
TestActivity: onResume
 

 

 

以上是关于Activity生命周期总结的主要内容,如果未能解决你的问题,请参考以下文章

横竖屏切换时activity生命周期变化的总结

横竖屏切换时activity生命周期变化的总结

基础总结篇之一:Activity生命周期

activity--生命周期总结

基础总结之一:Activity生命周期

基础总结之一:Activity生命周期