• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > [better practice系列]Android处理好activity正确情况下的生命周期和意外情况下的生命周期浅析

[better practice系列]Android处理好activity正确情况下的生命周期和意外情况下的生命周期浅析

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了[better practice系列]Android处理好activity正确情况下的生命周期和意外情况下的生命周期浅析等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

[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回调

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2017-05-26JSON解析和XML解析对比,JSON解析XML解析
  • 2017-05-26Android Call(打电话)的基本知识详解,androidcall
  • 2017-05-26Android 大杂烩工程之ListView的开发2以及数据仓库开发模式
  • 2017-05-26Android之SQLite数据存储,androidsqlite
  • 2017-05-262015年Android作品集
  • 2017-05-26AS下NDK开发(一),as下ndk开发
  • 2017-05-26活动的生命周期(五)活动的启动模式,生命周期模式
  • 2017-05-26【1】Android 学习笔记 【第一个安卓程序】,android安卓
  • 2017-05-26Android动画解析(一)—— Frame Animation(帧动画)
  • 2017-05-26硅谷新闻9--图片三级缓存,

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Android 7.0(牛轧糖)新特性,android牛轧糖
    • MAC下写入ntfs文件系统
    • MSM8909+Android5.1.1键盘驱动---sn7326介绍
    • VysorPro助手,vysorpro破解版
    • android 杀进程场景
    • Android 面试题--Activity,android--activity
    • [android] 手机卫士手势滑动切换屏幕,android手势
    • cordova开发自定义插件
    • 自定义轮播图片框架的使用,自定义播图片框架
    • 在Kotlin上怎样用Mockito2 mock final 类(KAD 23),kotlinmockito2

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有