`
reverocean
  • 浏览: 193000 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Android in Practice笔记第三章

阅读更多

第三章

<!--[if !supportLists]-->1.       <!--[endif]-->Application生命周期

每个Android的应用都有一个Application对象,我们可以使用该对象共享一些状态,即使我们不继承Application,提供自己的Application对象,Android平台也为我们提供一个默认的Application对象。

生命周期为:

<!--[if !supportLists]-->1)      <!--[endif]-->onCreate在应用程序开始的时候被调用。该方法一定要实现的快。

<!--[if !supportLists]-->2)      <!--[endif]-->onLowMemory在系统需要回收应用时调用。可以在这里清除缓存或者其他释放内存的操作,这样做的好处是如果你释放了足够多的内存,系统不会停止该应用。

<!--[if !supportLists]-->3)      <!--[endif]-->onTerminate在应用被停止时,偶尔被调用

<!--[if !supportLists]-->4)      <!--[endif]-->onConfigurationChanged当应用程序运行时设备配置发生变化时调用

Constant

Value

Description

mcc

0x0001

The IMSI MCC has changed, that is a SIM has been detected and updated the Mobile Country Code.

mnc

0x0002

The IMSI MNC has changed, that is a SIM has been detected and updated the Mobile Network Code.

locale

0x0004

The locale has changed, that is the user has selected a new language that text should be displayed in.

touchscreen

0x0008

The touchscreen has changed. Should never normally happen.

keyboard

0x0010

The keyboard type has changed, for example the user has plugged in an external keyboard.

keyboardHidden

0x0020

The keyboard or navigation accessibility has changed, for example the user has slid the keyboard out to expose it. Note that despite its name, this applied to any accessibility: keyboard or navigation.

navigation

0x0040

The navigation type has changed. Should never normally happen.

orientation

0x0080

The screen orientation has changed, that is the user has rotated the device.

screenLayout

0x0100

The screen layout has changed. This might be caused by a different display being activated.

uiMode

0x0200

The global user interface mode has changed. For example, going in or out of car mode, night mode changing, etc.

screenSize

0x0400

The current available screen size has changed. If applications don't target at least HONEYCOMB_MR2 then the activity will always handle this itself (the change will not result in a restart). This represents a change in the currently available size, so will change when the user switches between landscape and portrait.

smallestScreenSize

0x0800

The physical screen size has changed. If applications don't target at least HONEYCOMB_MR2 then the activity will always handle this itself (the change will not result in a restart). This represents a change in size regardless of orientation, so will only change when the actual physical screen size has changed such as switching to an external display.

fontScale

0x40000000

The font scaling factor has changed, that is the user has selected a new global font size.

 

<!--[if !supportLists]-->2.       <!--[endif]-->用户启动一个Android应用的时候,会使用一个唯一的用户ID去启动一个新的进程,这样就能让每个应用程序有独立的内存和状态,以及安全和多任务。

每个进程都有一个主线程(Main Thread),这个主线程一般被称为UI Thread

<!--[if !supportLists]-->3.       <!--[endif]-->Choose which processes get the ax

Android平台会尽可能的保证应用的进程存在,但是因为资源有限,系统会关闭一些进程。Android平台通过5个级别去找到哪个进程可以被关闭:

<!--[if !supportLists]-->1)      <!--[endif]-->Foreground:用户正在交互的

<!--[if !supportLists]-->2)      <!--[endif]-->Visible:这个不太明白

<!--[if !supportLists]-->3)      <!--[endif]-->Service:通过startService启动的Service

<!--[if !supportLists]-->4)      <!--[endif]-->Background:后台运行的Activity的进程。当有多个进程时,使用LRU

<!--[if !supportLists]-->5)      <!--[endif]-->Empty:没有和任何应用程序组件挂钩的进程

<!--[if !supportLists]-->4.       <!--[endif]-->通过设置android:process的属性,可以让一个应用跑在多个进程里或者多个应用跑在一个进程里。默认是每个应用一个进程,当多个应用为了更轻松的访问同一个文件时,可以让多个应用跑在一个进程里。但是多个应用跑在同一个进程里时,会增加快垃圾收集的次数,会影响应用的运行速度。

<!--[if !supportLists]-->5.       <!--[endif]-->Activity的生命周期

<!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0" /> <v:f eqn="sum @0 1 0" /> <v:f eqn="sum 0 0 @1" /> <v:f eqn="prod @2 1 2" /> <v:f eqn="prod @3 21600 pixelWidth" /> <v:f eqn="prod @3 21600 pixelHeight" /> <v:f eqn="sum @0 0 1" /> <v:f eqn="prod @6 1 2" /> <v:f eqn="prod @7 21600 pixelWidth" /> <v:f eqn="sum @8 21600 0" /> <v:f eqn="prod @7 21600 pixelHeight" /> <v:f eqn="sum @10 21600 0" /> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" /> <o:lock v:ext="edit" aspectratio="t" /> </v:shapetype><v:shape id="Picture_x0020_1" o:spid="_x0000_i1025" type="#_x0000_t75" alt="Activity生命周期记忆" style='width:442.5pt;height:221.25pt;visibility:visible; mso-wrap-style:square'> <v:imagedata src="file:///C:\Users\hehai\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png" o:title="Activity生命周期记忆" /> </v:shape><![endif]--><!--[if !vml]-->Activity生命周期记忆<!--[endif]-->

不难看出,其实这些方法都是两两对应的,onCreate创建与onDestroy销毁;onStart可见与onStop不可见;onResume可编辑(即焦点)与onPause;这6个方法是相对应的,那么就只剩下一个onRestart方法了,这个方法在什么时候调用呢?答案就是:在ActivityonStop后,但是没有被onDestroy,在再次启动此Activity时就调用onRestart(而不再调用onCreate)方法;如果被onDestroy了,则是调用onCreate方法。

这样大家就应该能够很容易的记住这些方法了。下面再通过一个比喻来看两个Activity的切换过程。

我们把Activity比作一本书,我们要看书,首先从书架上取出书(onCreate),然后放到桌上(onStart),接着打开书(onResume),这样我们就可以看书并可以在书本上写字了。

如果这时候我们要启动另一个Activity,也就是要看另一本书,首先我们放下手中的笔或者说合上书(onPause),然后从书架上拿下另一本书(书2onCreate),然后把书本2放到桌上并打开(书2onStartonResume)。

如果书本1被书本2完全盖住了,即不可见了,就调用书本1onStop;而如果书本2较小,没有完全盖住书本1,则不会调用。

我们还可以把书本1放回书架上,即onDestroy

另外,还有一点要注意,Activity在处于onPauseonStoponDestroy状态下,系统都可以销毁该Activity所在进程,所以我们在处理一些要保存的数据时,必须在onPause方法中进行,因为onStoponDestroy方法不一定会被调用。

<!--[if !supportLists]-->1)      <!--[endif]-->onCreateActivity第一次被创建时调用该方法。应该在这个方法里做所有的静态初始化:创建viewslist绑定数据等。该方法还提供包含了上次的状态的Bundle的参数,可以通过这个参数恢复ActivityAlways followed by  onStart.

<!--[if !supportLists]-->2)      <!--[endif]-->onRestart: Activitystop之后,没有被destory,但是又被用户调用时调用这个方法。Always followed by onStart.

<!--[if !supportLists]-->3)      <!--[endif]-->onStart: Activity变成可见时调用该方法。Followed by onResume if the activity comes to the foreground, or onStop if it becomes hidden.

<!--[if !supportLists]-->4)      <!--[endif]-->onResume: Activity开始跟用户交互的时候调用该方法。这时,这个Activityactivity栈的最上面。Always followed by onPause.    覆盖该方法去更新Activity的视图,是更新而不是重新创建。可以通过web service取数据然后刷新views

<!--[if !supportLists]-->5)      <!--[endif]-->onPause: 当系统开始恢复前一个activity的时候调用。在这个方法里一般用来提交未保存的改变到数据库,停止动画以及其他消耗CPU的事情。因为在这个方法未结束之前,要恢复的activity不会调用onResume方法,所以这个方法推荐很快就返回。Followed by either onResume if the activity returns to the front, or onStop if it becomes invisible to the user.  一般请覆盖该方法,在该方法里清楚Activity创建的东西,以回收内存。

<!--[if !supportLists]-->6)      <!--[endif]-->onStop: Activity因为其他Activity调用了onResume的方法而覆盖了本Activity不再被可见时调用。当一个新的Activity开始了或者已经存在的Activity被拿到前面时,或者当前这个Activitydestoryed时。Followed by either onRestart if this activity is coming back to interact with the user, or onDestory if this activity is going away.

<!--[if !supportLists]-->7)      <!--[endif]-->onDestory: 当该Activity结束时或者系统为了节约内存零时destoryactivity时会调用该方法。可以使用isFinishing方法来判断是上面的那一种情况。如果是因为回收内存而被destory,紧接着的是onRestart方法。

<!--[if !supportLists]-->6.       <!--[endif]-->Configuration

Configuration类定义了设备的所有配置信息。包括硬件配置,设备方向,屏幕大小,语言设置等等。

Configuration改变的时候,Androiddestoryrecreate当前的Activity

Configuration改变时,Android会调用onPauseonDestorydestory当前的Activity,然后调用当前的ActivityonCreateonResume方法重建当前的Activity。同时,instance state也通过onSaveInstanceStatonRestoreInstanceState这两个方法保存和恢复。

通过设置android:configChange的值,可以让Android不销毁和重建Activity

<!--[if !supportLists]-->7.       <!--[endif]-->Activity的实例状态(instance state

Instance state:没有提交的form表单,selectionsListviewindex等数据,与Activity的生命周期一样。

Persistent state

 

Instance stateAndroid系统destory一个Activity时会保存(Configuration改变或者别的)。在Activityfinish(按Back按钮)之后不会被保存。

 

ActivityonRestoreInstanceState(Bundle savedInstanceState)方法和onSaveInstanceState(Bundle outstate) 方法来恢复和保存Activity的实例状态(instance state

<!--[if !supportLists]-->8.       <!--[endif]-->Noconfiguration instance state

Noconfiguration instance state就是在Activity两个实例之间传递的state,因为Activity会被destory,然后重建Activity之后可以通过Noconfiguration instance state得到上一个实例的一些状态。

使用getLastNonConfigurationInstance方法取得上一个实例的noconfiguration instance state

使用onRetainNonConfigurationInstance方法返回你想传递的内容

需要注意的事情是实例之间传递的状态可以是任意对象,但是不要有当前Activity的引用,否则这样会导致Activity不能被回收,造成内存泄露。

不要随意使用

不要传递会跟configuration变化而变化的资源

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics