1.生命周期图

生命周期图

基本就是这样没啥可说的,记住就行了。另外附一些说明

  • The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single final call to onDestroy(). An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy(). For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy().

  • 整个生命周期为oncreate - ondestory 方法之间,在oncreate里做全部的初始化公里,比如后台运行的线程和下载数据,应该在oncreate里创建而在ondestory 里停止。

  • The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.

  • activity 的可见在onstart-onstop方法之间,在这期间用户能够在屏幕上看到这个activity,但是这个时候activity可能还不再前台同时用户可能还无法和这个activity交互,比如你可以在onstart里注册广播监听器用来监听UI的改变,在onstop里反注册掉广播当用户不再看到你展示的东西。这两个方法能够被多次调用,只要activity在visible和hidden状态之间切换。

  • The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.

  • 前台可见在onresume-onpause之间,在这期间activity处于其他所有activity之上并且正在和用户进行交互,activity的onresume和onpause 是非常频繁的调用的,比如当设备休眠,处理activity result,处理intent,所以这两个方法里应该足够轻量(不要做会卡顿UI的工作)

2.启动模式

  1. standard 标准模式,每次都启动新的activity,新启动的activity和启动它的activity在一个任务栈里,如果用application 启动activity,需要指定一个新的任务栈

  2. singleTop 栈顶复用模式,如果栈顶是activity A,再启动A则不会重新创建,而是调用A的onnewintent方法。如果栈顶不是A,则启动一个新的A。

  3. singleTask 栈内复用模式,栈内存在即复用,会自带clear top 属性,流程是先去寻找任务栈,任务栈存在再去里边寻找目标activity。有就调用,没有创建

  4. singleInstance,自己单独位于一个任务栈,全局只有一个实例

3. Intentfilter

  1. action
    • intent 中要存在action
    • action必须匹配过滤规则其中的一个
  2. category
    • intent 中可以没有category
    • intent中的每个category必须匹配过滤规则其中的某一个
  3. data

    • 有点复杂,用到再说 -.-

results matching ""

    No results matching ""