`
baobaoupup
  • 浏览: 470908 次
文章分类
社区版块
存档分类
最新评论

android 2.0 notes 2

 
阅读更多

Component Lifecycles

Activity lifecycle

一个PAUSER ACT是活着的,它维护所有的状态和成员信息并且 attached to the window manager,但是能被杀死在低内存状态下。

一个STOPPED ACT仍旧维护所有的状态和成员信息。

在ONCREATE中做初始化,一般也实现ONPAUSE去提交数据变化为了准备去停止与用户交互。

An activity does all its initial setup of "global" state in onCreate() , and releases all remaining resources in onDestroy()

如果有个背景线程去下载数据,他能被创建在ONCREATE中,停止在ONDESTROY

可视化的生命时期是在ONSTART和ONSTOP中。在这段时期,用户能看到这个ACT在屏幕上。(他也许不再前台和用户在交互)

在这俩个方法中,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 can no longer see what you are displaying.

这俩个方法能被调用多次在可视与不可视之间。

前景生命周期发生在ONRESUME,ONPAUSE之间。

在这俩个方法之间代码应该是轻量级的

onPause() This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns.

如果进程将被KILL,ONPAUSE将被保证调用,STOP DESTRO并不能保证,SO,你应该在这里持续化数据。

Saving activity state

当系统而不是用户 关闭ACT去节省内存,为了保存状态在ACT被杀之前,你能实现 onSaveInstanceState

系统调用这个方法在ACT将被摧毁之前(也就是在ONPAUSE之前),当ACT再一次被START,BUNDLE被传递到ONCREATE

也被传递到当调用ONSTART之后, onRestoreInstanceState() 里,因此这俩个方法都能重新创建这个状态。

onSaveInstanceState() and onRestoreInstanceState() are not lifecycle methods. They are not always called.

注意对于用户自己的行为 onSaveInstanceState 不将被调用,比如按了BACK键或者,FINISH这个ACT.

因为I他并不中被调用,因此不要在这存储持久化数据。

Service lifecycle

在ONCREATE中创建线程,打开音乐播放器,在ONSTART中拿到参数INTENT的去打开选播的歌曲。

The onCreate() and onDestroy() methods are called for all services, whether they're started by Context.startService() or Context.bindService() . However, onStart() is called only for services started by startService() .

Broadcast receiver lifecycle

A process with an active broadcast receiver is protected from being killed. But a process with only inactive components can be killed by the system at any time, when the memory it consumes is needed by other processes.

当这个广播信息被处理的消耗时间的,那么他应该被做在另一个分割的线程,离开与用户交互的主线程。

但是这里带来一个问题,如果ONRECEIVE创建了一个线程返回了the entire process, including the new thread, is judged to be inactive (unless other application components are active in the process) putting it in jeopardy of being killed

The solution to this problem is for onReceive() to start a service and let the service do the job, so the system knows that there is still active work being done in the process.


Processes and lifecycles

Processes with the lowest importance are eliminated first, then those with the next lowest, and so on. There are five levels in the hierarchy

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 pt</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin-top:0cm; margin-right:0cm; margin-bottom:10.0pt; margin-left:0cm; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:Calibri; mso-fareast-font-family:宋体; mso-bidi-font-family:"Times New Roman"; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} p.MsoAcetate, li.MsoAcetate, div.MsoAcetate {mso-style-noshow:yes; mso-style-link:" Char Char1"; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:9.0pt; font-family:Calibri; mso-fareast-font-family:宋体; mso-bidi-font-family:"Times New Roman"; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} span.CharChar1 {mso-style-name:" Char Char1"; mso-style-noshow:yes; mso-style-locked:yes; mso-style-link:"Balloon Text"; mso-ansi-font-size:9.0pt; mso-bidi-font-size:9.0pt; font-family:Calibri; mso-ascii-font-family:Calibri; mso-fareast-font-family:宋体; mso-hansi-font-family:Calibri; mso-ansi-language:EN-US; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:81998237; mso-list-template-ids:543044992;} @list l0:level1 {mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt;} @list l0:level2 {mso-level-number-format:bullet; mso-level-text:o; mso-level-tab-stop:72.0pt; mso-level-number-position:left; text-indent:-18.0pt; mso-ansi-font-size:10.0pt; font-family:"Courier New"; mso-bidi-font-family:"Times New Roman";} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> <!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!-- [endif]-->

Android 系统会尽可能长的延续一个应用程序进程,但在内存过低的时候,仍然会不可避免需要移除旧的进程。为决定保留或移除一个进程, Android 将每个进程都放入一个 重要性层次 中,依据则是它其中运行着的组件及其状态。重要性最低的进程首先被消灭,然后是较低的,依此类推。 重要性共分五层,依据重要性列表如下:

<!-- [if !supportLists]-->1. <!-- [endif]-->前台进程 是用户操作所必须的。当满足如下任一条件时,进程被认为是处于前台的:

<!-- [if !supportLists]-->o <!-- [endif]-->它运行着正在与用户交互的 activity Activity 对象的 onResume() 方法已被调用)。

<!-- [if !supportLists]-->o <!-- [endif]-->一个正在与用户交互的 activity 使用着它提供的一个服务。

<!-- [if !supportLists]-->o <!-- [endif]-->它包含着一个正在执行生命周期回调方法( onCreate() onStart() onDestroy() )的 Service 对象。

<!-- [if !supportLists]-->o <!-- [endif]-->它包含着一个正在执行 onReceive() 方法的 BroadcastReceiver 对象。

任一时间下,仅有少数进程会处于前台,仅当内存实在无法供给它们维持同时运行时才会被杀死。一般来说,在这种情况下,设备已然处于使用虚拟内存的状态,必须要杀死一些前台进程以用户界面保持响应。

<!-- [if !supportLists]-->2. <!-- [endif]-->可视进程 没有前台组件,但仍可被用户在屏幕上所见。当满足如下任一条件时,进程被认为是可视的:、

<!-- [if !supportLists]-->o <!-- [endif]-->它包含着一个不在前台,但仍然为用户可见的 activity (它的 onPause() 方法被调用)。这种情况可能出现在以下情况:比如说,前台 activity 是一个对话框,而之前的 activity 位于其下并可以看到。

<!-- [if !supportLists]-->o <!-- [endif]-->它包含了一个绑定至一个可视的 activity 的服务。

可视进程依然被视为是很重要的,非到不杀死它们便无法维持前台进程运行时,才会被杀死。

<!-- [if !supportLists]-->3. <!-- [endif]-->服务进程 是由 startService() 方法启动的服务,它不会变成上述两类。尽管服务进程不会直接为用户所见,但它们一般都在做着用户所关心的事情(比如在后台播放 mp3 或者从网上下载东西)。所以系统会尽量维持它们的运行,除非系统内存不足以维持前台进程和可视进程的运行需要。

<!-- [if !supportLists]-->4. <!-- [endif]-->背景进程 包含目前不为用户所见的 activity Activity 对象的 onStop() 方法已被调用)。这些进程与用户体验没有直接的联系,可以在任意时间被杀死以回收内存供前台进程、可视进程以及服务进程使用。一般来说,会有很多背景进程运行,所以它们一般存放于一个 LRU (最后使用)列表中以确保最后被用户使用的 activity 最后被杀死。如果一个 activity 正确的实现了生命周期方法,并捕获了正确的状态,则杀死它的进程对用户体验不会有任何不良影响。

<!-- [if !supportLists]-->5. <!-- [endif]-->空进程 不包含任何活动应用程序组件。这种进程存在的唯一原因是做为缓存以改善组件再次于其中运行时的启动时间。系统经常会杀死这种进程以保持进程缓存和系统内核缓存之间的平衡。

Android 会依据进程中当前活跃组件的重要程度来尽可能高的估量一个进程的级别。比如说,如果一个进程中同时有一个服务和一个可视的 activity ,则进程会被判定为可视进程,而不是服务进程。

此外,一个进程的级别可能会由于其它进程依赖于它而升高。一个为其它进程提供服务的进程级别永远高于使用它服务的进程。比如说,如果 A 进程中的内容提供者为进程 B 中的客户端提供服务,或进程 A 中的服务为进程 B 中的组件所绑定,则 A 进程最低也会被视为与进程 B 拥有同样的重要性。

因为运行着一个服务的进程重要级别总高于一个背景 activity 。所以一个 activity 以启动一个服务的方式启动一个长时间运行过程比简单的衍生一个线程来进行处理要好。尤其是当处理过程比 activity 本身存在时间要长的情况之下。我们以背景音乐播放和上传一个相机拍摄的图片至网站上为例。使用服务则不论 activity 发生何事,都至少可以保证操作拥有 服务进程 的权限。如上一节 广播接收器生命周期 所提到的,这也正是广播接收器使用服务,而不是使用线程来处理耗时任务的原因。

分享到:
评论

相关推荐

    Xamarin 2.0 Patcher

    1. Download and install the Xamarin Installer 2.0 from the Official Website. 2. Download and Run MFxPatcher. 3. Click Patch. ENJOY! If you like the software, buy it! PREREQUISITES & NOTES - This ...

    Android-ObservableScrollView

    Release notes FAQ Apps that use this library Badge Jair Player by Akshay Chordiya My Gradle by Erick Chavez Alcarraz ThemeDIY by Darkion Avey {Soft} Skills by Fanatic Devs If you're using this ...

    Android代码-大众点评开源的分布式服务通信框架(RPC)

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

    uNote:具有材料设计的Android Notes应用

    具有材料设计的Android Notes应用 该演示应用程序可在上找到。 Github上的开源-https: 特征 内联标签。 在笔记中的任何地方使用#hashtag和@mentions。 uNote会自动在边栏中将它们编入索引并将它们分组在一起,...

    notes:用于记笔记的 Android 应用

    笔记Android 笔记应用程序,仍处于开发初期。 支持文本注释、复选框、音频、照片和(很快)视频。 与 Froyo 和更高版本的设备兼容。 此应用中使用的 Android 功能: 活动碎片列表视图阵列适配器ViewPagers 对话框...

    Android--极简笔记App

    Material Design Notes App

    android_mynotes_app:保存您的重要笔记

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at ...

    书籍库:用于程序库的自由库

    书库 程序库创建者保护库。 Libros contenidos ... Android_Notes_For_Professionals(未读) BPMN_2.0_Manual_de_Referencia_y_Guia_Pra(未读) Programming_grails(未读) Ellucian_CRM_Recrui_4.9_New_In

    Notes:CMPUT 301 的笔记应用程序

    许可证 GPLv2+:GNU GPL 版本 2 或更高版本 。 这是免费软件:您可以自由更改和重新分发它。 在法律允许的范围内,不提供任何保证。 第三方库 amfontai Notes 使用以下库: Android 和 Android SDK(当然) O

    UWP-SDK:适用于通用Windows平台的Neo Smartpen SDK 2.0

    通用Windows平台(UWP)SDK 2.0 适用于Windows 10的UWP SDK。该库可让您将Neo smartpen集成到您的应用程序中。 SDK旨在使用Windows 10(UWP)的Neo smartpen。 SDK库已构建并发布到 关于Neo Smartpen Neo ...

    Android代码-支持格式自动识别和展示的 EditBox。

    Mask EditText Sample ...Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the

    Noted-Android:适用于Android的著名应用

    注意-注释和任务注意的是使用Kotlin,MVVM,Material design组件和Realm数据库构建的Notes and Tasks应用程序。特征:用文字样式书写笔记添加类别添加带有提醒的任务按类别或文本过滤注释和任务着色便签卡贡献:拉...

    PolygonBattleRoyale大逃杀资源

    AFPC 1.0 AdvancedHarvesting AdvancedKyMagicFX01 v1.1 Adventure Creator 1.65.2 Alpha Mask UI Sprites Quads v2.1 Amplify Shader Editor Andro id Etcetera Plugin v3.9 Android In App Billing Plugin v2.15 ...

    RTL8188C_8192C_USB_linux_v3.4.4_4749.20121105.zip

    User Guide for WPS2.0 ================================================================================================================== (*) Please use wpa_supplicant_hostapd-0.8_rtw_20120622....

    TMS Pack for FireMonkey2.3.0.1

    Support for Windows 32 bit, 64 bit, Mac OS X, iOS and Android Support for HTML formatted text, including hyperlinks in various parts of the components Built-in support for LiveBindings in ...

    appMobi游戏框架api帮助文档

    2.0 Programming Prerequisites ........................................................................................................ 4 3.0 Plugin Architecture .........................................

Global site tag (gtag.js) - Google Analytics