设为首页 加入收藏

TOP

喜闻乐见-Activity生命周期(一)
2019-09-01 23:14:40 】 浏览:67
Tags:喜闻乐见 -Activity 生命 周期

Activity的生命周期,对于Android开发者来说,再熟悉不过了。但是我们接触到的资料,绝大部分都只是谈了一些表面上的东西,例如各个回调的顺序等等。本文试图换个角度来讲解,也希望对各位读者有所帮助。

1. 生命周期

首先附上一张大家都熟悉的不能再熟悉的图了

Activity生命周期

对于各个流程的回调,想必大家早已熟记于心了,对于单个Activity来说,完全没问题,复杂点的,不就是转屏嘛?能有啥,好的,下面几个问题,麻烦大家先思考下。

  1. FirstActivity启动了SecondActivity,整个流程是怎样的?
  2. setContentView如果放在onStart或者onResume中,会有什么问题吗?
  3. onPause中可以保存状态,为什么还要onSaveInstanceState,onCreate中有恢复机制,为什么还需要onRestoreInstanceState?
  4. 如何判断一个Activity真正可见

2. 来自官方

在回答这些问题之前,先来回顾下Activity的各个阶段,下面的英文部分出自Google Android官方文档

2.1 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().

直接看重点,onCreate是用来干啥的,创建view、绑定data的地方。是初始化Activity的地方,setContentView以及获取控件都应该放在这里去做。

2.2 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.

onPause会被调用,是在Activity正在对用户变得可见的时候。也就是说这个时候,对于用户来说不是真正的可见,也不可去响应用户的输入。

2.3 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().

onResume是在将要可以产生交互的时候被调用的,也就是说,还不能响应用户的输入操作。在这个阶段,Activity已经是处在栈顶了。

2.4 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.

onPause可以用来干啥,停止动画或者那些占用CPU操作的地方,但是在onPause里面进行的操作,不能够太久,应该very quick,因为下一个Activity需要等onPause结束后才会被调起。这个very quick的时间,应该是多少了?这个在ActivityManagerService中有定义,不能超过500ms。如果在onPause里面处理时间超过5秒的话,会不会出现ANR呢?

// How long we wait until giving up on the last activity to pause.  This
// is short because it directly impacts the responsiveness of starting the
// next activity.
static final int PAUSE_TIMEOUT = 500;

2.5 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.

onStop的描述很简单,Activity对用户不可见时调用,但是文档后面Killable一栏显示的是YES,也就是说,onStop有可能会被强制结束掉而不完整的执行。

2.6 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.

onDestroy是在Activity要被释放掉时调用的,但是这个被释放,有主动的(手动去调用finish())和被动的(系统回收),可以通过isFinishing来区分这两种场景。

2.7 onSaveInstanceState

This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state.

The default implementation take

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android各版本特性 下一篇喜闻乐见-Android简介

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目