android 生命期说明的一些易忽略细节

2016-03-03  本文已影响56人  panberglee

来源

http://developer.android.com/training/basics/activity-lifecycle/index.html

生命周期图

launcher properties

If either theMAINaction orLAUNCHERcategory are not declared for one of your activities, then your app icon will not appear in the Home screen's list of apps.

onPause的用途

Stop animations or other ongoing actions that could consume CPU.

Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email). only save changes expected to be auto-saved by user.

Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.

onStop的用途

Although the onPause() method is called before onStop(), you should use onStop() to perform larger, more CPU intensive shut-down operations, such as writing information to a database.

Recreating Activity

Note:In order for the Android system to restore the state of the views in your activity,each view must have a unique ID, supplied by the android:id attribute.

哪些内容不需要onSaveInstanceState来存储,哪些一定要?

The default implementation of this method saves information about the state of the activity's view hierarchy, such as the text in an EditText widget or the scroll position of a ListView.

onCreate里面恢复的bundle和onRestoreInstanceState的区别?

1.onCreate不是recreate的流程中被调用时,Bundle参数为null所以在onCreate中恢复需要判空

2.非recreate流程不会调用到onRestoreInstanceState,所以此处恢复状态的Bundle参数不需要判空

3.Always call the superclass implementation of onRestoreInstanceState() so the default implementation can restore the state of the view hierarchy.

onDestroy不一定会被调用

原因 There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.stackoverflow的回复

Activity onDestroy() 调用研究 说明了一种不会调用onDestroy的场景,待验证

知乎给出的在onPause中判断isFinishing来提前释放onDestroy中要释放资源的方法

一些不太常用的回调

onUserLeaveHint回调的使用

其他Activity相关的知识积累

android context解析

出于安全原因的考虑,Android是不允许Activity或Dialog凭空出现的,一个Activity的启动必须要建立在另一个Activity的基础之上,也就是以此形成的返回栈。而Dialog则必须在一个Activity上面弹出(除非是System Alert类型的Dialog)

会有什么安全问题?

system alert类型的dialog有什么区别?

getApplication() getApplicationContext() getBaseContext()的区别

context的子类Activity Service Application等实例化ContextImpl的说明

上一篇下一篇

猜你喜欢

热点阅读