[better practice系列]Android处理好activity正确情况下的生命周期和意外情况下的生命周期浅析
前言:
Activity生命周期是每一个Android开发者接触Android之始就会学习的东西,每个人都会说出点什么,我想任何一个经验老道高级语言开发程序员谈到生命周期这个概念,总有一种接触越多体会越深的感觉。
Android是面向对象设计的,Android是面向对象设计的,Android是面向对象设计的。重要的事情先说三遍。Activity到底是对什么的抽象,我想大家都不敢随随便便自己给一个定义。
正文:
首先先贴一张官方正版的生命周期图:
这个图反映了android在正常情况下activity的生命周期变化。
OK我们先简单的看一下官方对Activity的描述:
An activity is a single, focused thing that the user can do. Almost allactivities interact with the user, so the Activity class takes care of
creating a window for you in which you can place your UI with{@link #setContentView}. While activities are often presented to the user
as full-screen windows, they can also be used in other ways: as floatingwindows (via a theme with {@link android.R.attr#windowIsFloating} set)
or embedded inside of another activity (using {@link ActivityGroup}).
意思呢就是:activity是单一的、用户可以操作的东西,基本上所有的activity都和用户交互,所以activity关心创建一个你可以放置UI的视窗。……
我们可以想象一下activity是对什么的抽象,而且定义的生命周期关键点就是这个东东的某些特殊状态点。
那我们再回顾一下基础知识:
activity生命周期中几个关键点所对应的实际场景
1.onCreate
activity开始被创建
2.onRestart
activity被重新启动
3.onStart
activity被启动
4onResume
activity进入可见、可交互状态
5onPause
activity开始暂停
6onStop
activity即将停止
7onDestroy
activity即将销毁
生命周期的演变在图中表现的很详细,再引用google官方的一个表:
Method |
Description |
Killable? |
Next |
onCreate() |
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart(). |
No |
onStart() |
onRestart() |
Called after your activity has been stopped, prior to it being started again. Always followed by onStart() |
No |
onStart() |
onStart() |
Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden. |
No |
onResume() or onStop() |
onResume() |
Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it. Always followed by onPause(). |
No |
onPause() |
onPause() |
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns. Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user. |
Yes |
onResume() or onStop() |
onStop() |
Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed. Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away. |
Yes |
onRestart() or onDestroy() |
onDestroy() |
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. |
Yes |
Nothing |
表格中用语言描述了生命周期可能的变化,其实还是看图明显。接下来集中写以下几个问题:
正常情况下各个生命周期转折点做点什么一些不合理的做法和不合理的原因异常情况下生命周期
正常情况下各个生命周期转折点做点什么
onCreate(Bundle savedInstanceState)
Called when the activity is starting. This is where most initialization should go: calling setContentView(int) toinflate the activity's UI, using findViewById to programmatically interact withwidgets in the UI, calling managedQuery(android.net.Uri, String[], String,String[], String) to retrieve cursors for data being displayed, etc.
You can call finish from within this function, in which case onDestroy() willbe immediately called without any of the rest of the activity lifecycle(onStart, onResume, onPause, etc) executing.
以上是API描述,onCreate中应该做一些初始化的工作,而且是绝大多是的实例化工作,言下之意是这里很适合做实例化工作(应该做),但存在有些特殊的情况(看到这里你是不会知道特殊情况是什么的)。而且这些初始化工作主要和界面有关,很多人要说,此处不要做耗时操作,是的,因为界面还没有可见,耗时操作会托节奏,但这是主要原因吗?可能阻塞主线程的耗时操作都是不应该在主线程中进行,做一些实例化的行为还谈不上耗时,哪怕是反射实现的。
void android.app.Activity.onStart()
Called after onCreate — or after onRestart when the activity had been stopped, but is now again being displayed to the user. It will be followed by onResume.
如果从正常的生命周期来看,总会觉得去实现onStart有点多余,需要注意的是,界面此时还是不可见的。不可交互的。不要去做动画。(不能确定是否是容错机制,在此处开始一个动画没有遇到过crash或者警告,但起码这时候是看不到的嘛,那就尽量不要在此处做不合理的事情)。
void android.app.Activity.onResume()
Called after onRestoreInstanceState,onRestart, or onPause, for your activity to start interacting with the user. This is a good place to begin animations,open exclusive-access devices (such as the camera), etc.
Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged to know for certain that your activity is visible to the user (for example, to resume a game).
一切和用户交互的准备应当在此前完成,例如添加各种监听器,尽量不要拖到这时候再处理。正如API所说,这时候是打开独占设备或者展示动画的好时机。但是注意一件事情:此时可能处于锁屏2333,用onWindowFocusChanged回调