Android-Activity生存周期

这里写图片描述

方法说明
void onCreate(Bundle savedInstanceState)Called when the activity is starting.
void onStart()Called after onCreate(Bundle) — or after onRestart() when the activity had been stopped, but is now again being displayed to the user.
void onRestart()Called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it).
void onResume()Called after onRestoreInstanceState(Bundle), onRestart(), or onPause(), for your activity to start interacting with the user.
void onPause()Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed.
void onStop()Called when you are no longer visible to the user.
void onDestroy()Perform any final cleanup before an activity is destroyed.
void onPostCreate(Bundle savedInstanceState)Called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called).
void onPostResume()Called when activity resume is complete (after onResume() has been called).
void onRestoreInstanceState(Bundle savedInstanceState)This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState.
void onSaveInstanceState(Bundle outState)Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).
void onUserLeaveHint()Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice.

onCreate:

Activity创建时调用。

onRestart:

Activity由stop状态重新启动时调用。

onStart:

Activity启动时 与 其被完全隐藏后重新启动时调用。

onResume:

Activity启动时 与 其被隐藏后重新启动时调用。

onPause:

Activity被隐藏时调用。

onStop:

Activity被完全隐藏时调用。


通过在每一个方法中添加一个Log来显示信息


一项测试(新开启的Activity完全隐藏):

当开启Activity-Survival后:

这里写图片描述
Activity的创建过称为:onCreate,onStart,onResume。

当开启新的Activity-SurvivalSecond后:

这里写图片描述
当在一个已存在的Activity上创建新的NewActivity时,Activity先onPause,之后启动NewActivity的创建过程——onCreate,onStart,OnResume,之后才启动Activity的onStop。

返回之前的Activity-Survival:

这里写图片描述
NewActivity创建之后,执行返回Activity的过程,NewActivity先onPause,之后启动Activity的恢复过程onRestart,onStart,onResume,之后才启动Activity的onStop与onDestroy。

关闭Activity-Survival:

这里写图片描述
Activity的关闭过称为:onPause,onStop,onDestroy。


另一项测试(新开启的Activity部分隐藏):

我将survivalSecond改为Dialog的模式。
AndroidManifest中设置android:theme=”@android:style/Theme.Dialog”
启动survivalSecond后再将其关闭Log中的记录为:
这里写图片描述
survival只是进行了onPause与onResume,并没有出现onStop,onStart。


按下home键

当开启Activity后按下home键:onPause,onStop.
当重新进入Activity后:onRestart,onStart,onResume.


按下锁屏键

当开启Activity后按下锁屏键:onPause
当再次按下锁屏键开启屏幕:onResume


onSaveInstanceState与onRestoreInstanceState

正如前面所介绍,onSaveInstanceState启动在Activity被杀死之前(我所理解的杀死(be killed)即onStop),用于存储必要的信息;onRestoreInstanceState启动在onStart之前用于恢复onSaveInstanceState存储的信息。
注意:onRestoreInstanceState被调用的前提是,activity“确实”被系统销毁了,而如果仅仅是停留在有这种可能性的情况下,则该方法不会被调用,例如,当正在显示activity的时候,用户按下HOME键回到主界面,然后用户紧接着又返回到activity,这种情况下activity一般不会因为内存的原因被系统销毁,故activity的onRestoreInstanceState方法不会被执行。


onPostCreate与onPostResume

onPostCreate启动在onCreate与onRestoreInstanceState完成之后
onPostResume启动在onResume完成之后


onUserLeaveHint

Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice.

For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity’s onPause() callback.

This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值