android应用当中Activity间相互跳转比较常见。本文将通过例子跟踪一下2个Activity间跳转过程中生命周期的变化过程。
整个操作过程如下图所示:
1. 打开应用,自动开启First Activity;
2. 按下“Jump Button”按钮,打开Second Activity;
3. 按下“回退”按钮,退出Second Activity,自动回到First Activity;
4. 按下“回退”按钮,退出应用,自动关闭First Activity。
注:两个Activity的启动模式都是默认的(standard)
在Fist Activity和Second Activity的相应“过程”方法里打印Log。这些“过程”方法包括:onCreate、onRestart、onStart、onResume、onPause、onStop、onDestroy。
打印结果如下
//----------1------------ First Activity: onCreate First Activity: onStart First Activity: onResume //----------2------------ First Activity: onPause Second Activity: onCreate Second Activity: onStart Second Activity: onResume First Activity: onStop //----------3------------ Second Activity: onPause First Activity: onRestart First Activity: onStart First Activity: onResume Second Activity: onStop Second Activity: onDestroy //----------4------------ First Activity: onPause First Activity: onStop First Activity: onDestroy |
结果分析:
1. 步骤1(打开First Activity):经过onCreate、onStart、onResume后First Activity就展现啦;
2. 步骤2(跳转至Second Activity):首先First Activity暂停(onPause),接下来Second Activity展现(onCreate、onStart、onResume),最后First Activity停止(onStop);
3. 步骤3(返回到First Activity):首先Second Activity暂停(onPause),接下来First Activity重新打开并展现(onRestart、onStart、onResume),最后Second Activity停止并销毁(onStop、onDestroy);
4. 步骤4(退出First Activity):经过onPause、onStop、onDestroy后First Activity暂停、停止并最终销毁。