前言:

为了形象,本文会做一些比喻,对应关系如下:
管家 :                 Android 系统 ;
老爷 :                 用户 ;
家庭经济情况 : 内存;
进程 :      仆人。

进程生命周期 ,又名 你丑你先死

Android家里养着许多仆人,正常情况下管家希望仆人都健健康康的活着,多给老爷挣点工分,博老爷欢心。
然而天不遂人愿,老爷骄奢无度,看见漂亮的仆人就往家里招,到处留情。日复一日,终于有一天,管家在开会时宣布:咱家存折里没钱了,养不下你们这么多人,你们每个都很优秀,哪个都是老爷的心头肉,可是没办法啊,为了保证大多数人活下去,你们中间有些人要先我们一步而去了嘤嘤嘤%>_<%。
听到这个消息,大家一片哗然,个个都觉得自己为老爷做出了贡献,命不该绝。
“肃静!”管家发话,“我跟老爷已经商量过了,所有人都会按照给老爷做的贡献多少分类,主要分下面四类,都仔细听着,我会按照重要性从高到低的顺序念出来!”
1. 前台Activity所在的进程 --- 当前用户可以看到、并且可以交互:
最重要的肯定最得老爷疼爱的大老婆,老爷天天见你、玩你,怎么舍得失去你呢。要是非问我你什么时候会被终结?放心吧,那得到最后的最后实在是没办法了才终结。通常这时候家里穷的基本一干二净了,下面那么几类人都奉献完了,为了保证老爷能有吃的,才会忍痛割爱。不过这种情况一般很少,您就放心吧。
2. 可视Activity所在的进程 --- 能看到,但是不能交互,比如说在Activity A 中点击按钮弹出一个对话框,这时Activity A 在对话框的阴影后面,虽然能看到,但是不能和用户交互:
能干的奴婢,因为很“能干”,虽然现在老爷不翻你牌,稍后他肯定耐不住寂寞会去找你玩的,你地位只比上面那位低一点,除非家里穷的实在没办法了,为了让大老婆活只能把你饿死这种情况,一般不会让你受委屈的,好好干。
3. 后台Activity所在的进程 --- 已经切换到后面的Activity、被暂停:
年老朱黄的仆人,虽然你曾经照顾过少爷,现在老了看不见了,也没什么用,没那么重要了。当家里没钱时,就会“被奉献”出自己占的地方给大老婆或者其他能干的仆人。突然有天老爷回心转意了,要见老仆人,怎么办?赶紧吃个大还丹重新调用onCreate(Bundle),如果“被奉献”前调用过神技“onSaveInstanceState(Bundle)”,把记忆存储起来,复活时的她就跟之前的她一样,没有丝毫变化,没有丝毫怨言。
"我就知道你会回来的"--- from background activity
4. 空进程 --- 里面没有运行Activity、Service或者BroadcastReceiver等应用组件:
新来的仆人,没给老爷做出过贡献,当遇到紧急情况时最先“被奉献”。

总结:
看到了吧,了解这些"潜规则"后,要想让你的程序在年老(进入Background)时过个安稳年,就得把你在Activity结束后还要进行的操作 放到Service或者BroadcastReceiver里,那样系统才知道你的操作很重要,为你保驾护航。
正如之前在Activity生命周期里说的那样,到底要关闭哪个进程很大程度上决定于 用户 是否跟这个进程有交互。
有些时候我们可能在一个Activity里会开启一个持续的操作,我们希望这个操作能够不被Activity生命周期影响。
比如说一个运行用户拍照后上传图片到网站的程序,可能一次上传很多图片,用户希望在退出程序后它能一直上传。
为了实现这个需求,我们需要在该Activity中开启一个Service来后台上传,当上传操作是运行是一个Service中时,系统就会意识到它的重要性(起码比那些“年老朱黄的仆”人还重要),不管开启操作的activity是否paused、stopped、finished,都会保证上传操作的正常运行。
官方文档:

Process Lifecycle

The Android system attempts to keep application process around for as long as possible, but eventually will need to remove old processes when memory runs low. As described in Activity Lifecycle, the decision about which process to remove is intimately tied to the state of the user's interaction with it. In general, there are four states a process can be in based on the activities running in it, listed here in order of importance. The system will kill less important processes (the last ones) before it resorts to killing more important processes (the first ones).

  1. The foreground activity (the activity at the top of the screen that the user is currently interacting with) is considered the most important. Its process will only be killed as a last resort, if it uses more memory than is available on the device. Generally at this point the device has reached a memory paging state, so this is required in order to keep the user interface responsive.

  2. visible activity (an activity that is visible to the user but not in the foreground, such as one sitting behind a foreground dialog) is considered extremely important and will not be killed unless that is required to keep the foreground activity running.

  3. background activity (an activity that is not visible to the user and has been paused) is no longer critical, so the system may safely kill its process to reclaim memory for other foreground or visible processes. If its process needs to be killed, when the user navigates back to the activity (making it visible on the screen again), its onCreate(Bundle) method will be called with the savedInstanceState it had previously supplied inonSaveInstanceState(Bundle) so that it can restart itself in the same state as the user last left it.

  4. An empty process is one hosting no activities or other application components (such as Service or BroadcastReceiver classes). These are killed very quickly by the system as memory becomes low. For this reason, any background operation you do outside of an activity must be executed in the context of an activity BroadcastReceiver or Service to ensure that the system knows it needs to keep your process around.

Sometimes an Activity may need to do a long-running operation that exists independently of the activity lifecycle itself. An example may be a camera application that allows you to upload a picture to a web site. The upload may take a long time, and the application should allow the user to leave the application while it is executing. To accomplish this, your Activity should start a Service in which the upload takes place. This allows the system to properly prioritize your process (considering it to be more important than other non-visible applications) for the duration of the upload, independent of whether the original activity is paused, stopped, or finished.

【趣读官方文档】1.管家的抉择 (Android进程生命周期)相关推荐

  1. 【读官方文档,学原味技术】SpringBoot-Staters和自定义Starter

    spring-boot-reference 如果不想阅读英文原文,你可以直接读本文的[TS]标注参考翻译内容.由于本人水平有限,如有理解错误,烦请指正,互相交流. Lire les documents ...

  2. android mvvm官方文档,MVVM: 这是一个android MVVM 框架,基于谷歌dataBinding技术实现

    MVVM 这是一个android MVVM 框架,基于谷歌dataBinding技术实现.dataBinding 实现的 V 和 VM的关联:使用IOC架构实现了 M 和 V的关联. 框架具有以下功能 ...

  3. Poco 库开发-教你如何读Poco的官方文档

    引言 在使用任何一种框架开发的时候,都不免要阅读官方的文档. 下面就讲述如何来读Poco库的官方文档 Poco库 在我们下载好Poco 后,会有两个文件夹,一个是放文档的doc,另外一个就是代码了,这 ...

  4. oracle12c官方文档中文版_三分钟让你真正读懂oracle12c 中cdb pdb概念及原理

    Oracle 12C引入了CDB与PDB的新特性,在ORACLE 12C数据库引入的多租用户环境(Multitenant Environment)中,允许一个数据库容器(CDB)承载多个可插拔数据库( ...

  5. 使用cephadm部署单节点ceph集群,后期可扩容(基于官方文档,靠谱,读起来舒服)

    目录 ceph各种部署工具比较(来自官方文档的翻译,靠谱!) 材料准备 cephadm使用条件 服务器有外网访问能力 服务器没有外网访问能力 安装cephadm cephadm的功能 两种安装方式 基 ...

  6. OpenCV-Python官方文档学习笔记(上)

    整理自OpenCV-Python官方文档 一. OpenCV-Python Tutorials 1 安装及验证 2 图片读写,展示 3 视频读写,展示 4 绘图功能(绘制几何形状:线.圆.椭圆.矩形. ...

  7. Swift 4官方文档中文版 The Basic(上)

    Swift学习交流群: 313838956 本群由Guards翻译组创建并维护, 志于给认真想学习Swift的同学打造一个良好的交流圈子. 该文章翻译自Apple官方文档: The Swift 4 P ...

  8. spark之4:基础指南(源自官方文档)

    spark之4:基础指南(源自官方文档) @(SPARK)[spark, 大数据] spark之4基础指南源自官方文档 一简介 二接入Spark 三初始化Spark 一使用Shell 四弹性分布式数据 ...

  9. dubbo官方文档_不可忽视的Dubbo线程池

    问题描述 线上突然出现Dubbo超时调用,时间刚好为Consumer端设置的超时时间. 有好几个不同的接口都报超时了 第1次调用超时,第2次(或第3次)重试调用非常快(正常水平) Dubbo调用超时的 ...

  10. Qt官方文档阅读笔记-对官方Star Delegate Example实例的解析

    对应的博文为: 目录 Star Delegate Example StarDelegate Class Definition StarDelegate Class Implementation Sta ...

最新文章

  1. SAP MM ME21N 创建委外采购PO报错 - Not possible to determine any components - 之对策
  2. 基于ESP32的智能车竞赛新版裁判系统的软件功能要求与实现
  3. 分享个人预算系统源码(含说明文档)
  4. You must provide a username via either --os-username or env[OS_USERNAME]
  5. 大话设计模式笔记 享元模式
  6. python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑
  7. 织梦dedecms响应式自媒体图片新闻资讯网站模板
  8. Jsoup实现java模拟登陆
  9. 硬盘格式化了的数据找到方案
  10. error: Setup script exited with error: Unable to find vcvarsall.bat
  11. 小程序消息订阅发送功能教程
  12. 计算机文件夹加密文件,电脑文件夹怎么加密,制作隐私的加密文件夹软件
  13. java 实体类校验_实体类的验证
  14. Java面试手册——高频问题总结(二)
  15. “跨次元”检测模型hold住各种画风,真人赛博,在线Demo可玩
  16. 详解GMT CST UTC DST PDT PST几个时间概念
  17. 获取手机电池百分比和电池容量方法
  18. 2019读过的好书推荐
  19. matlab likelihood,Matlab做空间面板模型log-likelihood为NAN怎么办
  20. 【MacOS】虚拟机Vmware安装MacOS

热门文章

  1. MIMO系统信道容量分析
  2. 使用ARCGIS多重缓冲区分析工具建立颜色渐变行政边界
  3. 数据分析师和数据工程师的区别是什么?
  4. PotPlayer没有声音解决方案
  5. 用Python爬取网页数据,手把手教会你!
  6. Tomcat配置优化(一)
  7. Android 梯形进度条、下载进度条;
  8. 清除计算机策略,怎么删除组策略
  9. eRPC:修改erpcgen代码生成模板增加#if#endif宏定义,解决多个eRPC服务共用时类型重复定义问题
  10. 操作系统系列(三)——编译和链接