生命周期
- onCreate()
- onStart()
- onResume()
- onPause()
- onStop()
- onDestroy()
保存和恢复状态
onSaveInstanceState()
onRestoreInstanceState()
注意:当用户显式关闭 Activity 时,或者在其他情况下调用 finish() 时,系统不会调用 onSaveInstanceState()
Activity间导航
startActivity()
startActivityForResult()
任务和返回栈
任务是用户在执行某项工作时与之互动的一系列 Activity 的集合。这些 Activity 按照每个 Activity 打开的顺序排列在一个返回堆栈中。
Android 7.0(API 24)及更高版本支持多窗口环境,当应用在这种环境中同时运行时,系统会单独管理每个窗口的任务;而每个窗口可能包含多项任务。
- Android 7.0支持分屏模式。
- Android 8.0 开始,应用可以将自身置于画中画模式,从而使其能在用户浏览其他应用或与之交互时继续显示内容。
启动模式
- standard
- singleTop
- singleTask
- singleInstance
activity 标签
<activity android:allowEmbedded=["true" | "false"]
android:allowTaskReparenting=["true" | "false"]
android:alwaysRetainTaskState=["true" | "false"]
android:autoRemoveFromRecents=["true" | "false"]
android:banner="drawable resource"
android:clearTaskOnLaunch=["true" | "false"]
android:colorMode=[ "hdr" | "wideColorGamut"]
android:configChanges=["mcc", "mnc", "locale",
"touchscreen", "keyboard", "keyboardHidden",
"navigation", "screenLayout", "fontScale",
"uiMode", "orientation", "density",
"screenSize", "smallestScreenSize"]
android:directBootAware=["true" | "false"]
android:documentLaunchMode=["intoExisting" | "always" |
"none" | "never"]
android:enabled=["true" | "false"]
android:excludeFromRecents=["true" | "false"]
android:exported=["true" | "false"]
android:finishOnTaskLaunch=["true" | "false"]
android:hardwareAccelerated=["true" | "false"]
android:icon="drawable resource"
android:immersive=["true" | "false"]
android:label="string resource"
android:launchMode=["standard" | "singleTop" |
"singleTask" | "singleInstance"]
android:lockTaskMode=["normal" | "never" |
"if_whitelisted" | "always"]
android:maxRecents="integer"
android:maxAspectRatio="float"
android:multiprocess=["true" | "false"]
android:name="string"
android:noHistory=["true" | "false"]
android:parentActivityName="string"
android:persistableMode=["persistRootOnly" |
"persistAcrossReboots" | "persistNever"]
android:permission="string"
android:process="string"
android:relinquishTaskIdentity=["true" | "false"]
android:resizeableActivity=["true" | "false"]
android:screenOrientation=["unspecified" | "behind" |
"landscape" | "portrait" |
"reverseLandscape" | "reversePortrait" |
"sensorLandscape" | "sensorPortrait" |
"userLandscape" | "userPortrait" |
"sensor" | "fullSensor" | "nosensor" |
"user" | "fullUser" | "locked"]
android:showForAllUsers=["true" | "false"]
android:stateNotNeeded=["true" | "false"]
android:supportsPictureInPicture=["true" | "false"]
android:taskAffinity="string"
android:theme="resource or theme"
android:uiOptions=["none" | "splitActionBarWhenNarrow"]
android:windowSoftInputMode=["stateUnspecified",
"stateUnchanged", "stateHidden",
"stateAlwaysHidden", "stateVisible",
"stateAlwaysVisible", "adjustUnspecified",
"adjustResize", "adjustPan"] >
<intent-filter>
<meta-data>
<layout>
. . .
</activity>
常见问题
生命周期相关
- 两个 Activity 之间跳转时执行的方法?
- 横竖屏切换时,Activity 各种情况下的生命周期?
- Activity 与 Fragment 之间生命周期比较?
- Activity 上有 Dialog 时按 Home 键时的生命周期?
- 前台切换到后台,然后再回到前台,Activity 生命周期回调方法?弹出 Dialog,生命周期回调方法?
其他
- Activity 状态保存与恢复?
- Fragment 状态保存?
- Application 和 Activity 的 Context 对象的区别?