详解Activity的生命周期

Activity的生命周期是很基础,也是面试经常遇到的问题,我们光知道有哪些周期是远远不够的,还要尽可能多的知道各种情况下生命周期的回调情况。
对于基本的调用流程本篇博客不会涉及,主要是讲解一下各种情况的调用情况。

一、ActivityA to ActivityB to ActivityA

ActivityA to ActivityB

ActivityA : onPause

ActivityB: onCreate
ActivityB: onStart
ActivityB: onResume

ActivityA : onStop

ActivityB to ActivityA

ActivityB : onPause

ActivityA: onRestart
ActivityA: onStart
ActivityA: onResume

ActivityB : onStop
ActivityB : onDestroy

二、ActivityA to ActivityB(透明的) to ActivityA

ActivityA to ActivityB

这时A不会执行onStop()

ActivityA : onPause

ActivityB: onCreate
ActivityB: onStart
ActivityB: onResume

ActivityB to ActivityA

ActivityB: onPause

ActivityA : onResume

ActivityB: onStop
ActivityB: onDestroy

三、转屏

转屏会重新创建当前的Activity,可以在onSaveInstanceState里保存相关信息
ActivityA: onPause
ActivityA: onSaveInstanceState
ActivityA: onStop
ActivityA: onDestroy

ActivityA: onCreate
ActivityA: onStart
ActivityA: onRestoreInstanceState
ActivityA: onResume

四、ActivityA(singleTop) to ActivityA

这主要是为了查看onNewIntent()何时调用
ActivityA: onPause
ActivityA: onNewIntent
ActivityA: onResume

ActivityA to ActivityB to ActivityA
1、activitya start activityb

2、activityb start activitya

在第二步被执行后,activitya就会顺序执行 onNewIntent onRestart onStart onResume

你可能感兴趣的:(android)