在android应用中, Activity是最重要的组件, 其生命周期(Lifecycle)被大家所熟知.
可是, 大家须要注意一些细节, 才干在使用中得心应手.
Android的生命周期改变主要有两种情况, 一是用户參与Activity生命周期的改变, 二是系统回收或配置改动导致的改变.
本文演示样例的GitHub下载地址
Android的生命周期:
1. 用户參与
用户最常见的两种操作, 按Home键或Back键.
(1) 点击Home键, 暂停当前Activity, 调用onPause -> onStop; 再次返回, 恢复当前页面, 调用onRestart -> onStart -> onResume.
(2) 点击Back键, 关闭当前Activity, 调用onPause -> onStop -> onDestroy; 再次启动则为重建, 从onCreate開始调用.
onStart()是Activity可见, 无法交互; onResume()是Activity可见, 能够交互. onPause是Activity无法交互, 必须运行前一个Activity的onPause完毕, 后一个Activity才干启动, 在onPause中, 不能运行复杂的操作, 否则会影响下一个Activity启动速度. onStop是Activity关闭显示.
注意: 启动透明页面仅仅调用onPause, 不会调用onStop, 即仅仅是不能够交互, 但仍在前台显示. 启动非透明页, 会调用onPause -> onStop.
运行操作, 启动透明页, 关闭透明页, 启动非透明页, 关闭非透明页.
E/DEBUG-WCL: MainActivity: onPause
E/DEBUG-WCL: MainActivity: onSaveInstanceState
E/DEBUG-WCL: TranslateActivity: onPause
E/DEBUG-WCL: MainActivity: onResume
E/DEBUG-WCL: TranslateActivity: onStop
E/DEBUG-WCL: TranslateActivity: onDestroy
E/DEBUG-WCL: MainActivity: onPause
E/DEBUG-WCL: SecondActivity: onCreate
E/DEBUG-WCL: MainActivity: onSaveInstanceState
E/DEBUG-WCL: MainActivity: onStop
E/DEBUG-WCL: SecondActivity: onPause
E/DEBUG-WCL: MainActivity: onRestart
E/DEBUG-WCL: MainActivity: onStart
E/DEBUG-WCL: MainActivity: onResume
E/DEBUG-WCL: SecondActivity: onStop
E/DEBUG-WCL: SecondActivity: onDestroy
注意透明页启动, 主页仅仅会调用onPause, 而不调用onStop.
onSaveInstanceState方法, 在关闭时, 都会运行, 但非系统调用启动, 不会运行恢复数据的操作, 即onRestoreInstanceState方法.
2. 系统重建
在系统配置发生改变时, Activity就会重建, 因为是系统控制的关闭和启动, 会调用onSaveInstanceState保存数据, onRestoreInstanceState恢复数据.
E/DEBUG-WCL: MainActivity: onPause
E/DEBUG-WCL: MainActivity: onSaveInstanceState
E/DEBUG-WCL: MainActivity: onStop
E/DEBUG-WCL: MainActivity: onDestroy
E/DEBUG-WCL: MainActivity: onCreate
E/DEBUG-WCL: MainActivity: onStart
E/DEBUG-WCL: MainActivity: onRestoreInstanceState
E/DEBUG-WCL: MainActivity: onResume
onRestoreInstanceState在能够交互之前(onResume)运行, onSaveInstanceState在停止交互(onPause)之后运行.
系统会保存关闭之前的视图结构和暂存数据. 视图详细保存的数据项, 能够通过阅读文档, 也能够直接阅读代码. 搜索视图类的onSaveInstanceState方法, 类SavedState就是保存的数据.
TextView的onSaveInstanceState
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
// Save state if we are forced to
boolean save = mFreezesText;
int start = 0;
int end = 0;
if (mText != null) {
start = getSelectionStart();
end = getSelectionEnd();
if (start >= 0 || end >= 0) {
// Or save state if there is a selection
save = true;
}
}
if (save) {
SavedState ss = new SavedState(superState);
// XXX Should also save the current scroll position!
ss.selStart = start;
ss.selEnd = end;
if (mText instanceof Spanned) {
Spannable sp = new SpannableStringBuilder(mText);
if (mEditor != null) {
removeMisspelledSpans(sp);
sp.removeSpan(mEditor.mSuggestionRangeSpan);
}
ss.text = sp;
} else {
ss.text = mText.toString();
}
if (isFocused() && start >= 0 && end >= 0) {
ss.frozenWithFocus = true;
}
ss.error = getError();
if (mEditor != null) {
ss.editorState = mEditor.saveInstanceState();
}
return ss;
}
return superState;
}
TextView主要保存了内容Text和焦点Focus, 另一些位置和错误信息.
假设是恢复自己定义数据, 恢复时机, 能够选择onCreate或onRestoreInstanceState, 差别是onCreate须要推断savedInstanceState是否为空, 而onRestoreInstanceState的參数savedInstanceState是一定非空, 否则不会调用. 推荐使用onRestoreInstanceState.
onCreate判空
if (savedInstanceState != null) {
String txt = savedInstanceState.getString(EXTRA_TEXT);
Log.e(TAG, "[onCreate]savedInstanceState: " + txt);
}
在AndroidManifest的Activity属性中加入
android:configChanges="orientation|screenSize"
防止在旋转屏幕时, 重绘页面, 但屏幕仍能够旋转.
不会调用生命周期, 而是调用onConfigurationChanged处理旋转屏幕事件.
使用android:screenOrientation=”portrait”会使屏幕保持竖直, 无法旋转.
动画
OK, that’s all! Enjoy it!