五、剖析Activity应用程序

我学习知识有个习惯,要是有源码的我话,我都要先看懂源码,然后一步一步地学习.

下面来看一下Activity源代码是怎么样的吧!

1、Activity源码

 源码由于太多了,复制过来看着不舒服!有要的留个言……

2、Activity是什么?

官方文档描述的比较详细,市面上的书籍关于Activity的一部分大都是从官方文档上翻译的。

官方文档描述如下:

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating set) or embedded inside of another activity (using ActivityGroup)。

 3、创建一个活动(Activity)代码

4、在清单(AndroidManifest.xml)中声明一个Activity代码

 

5、活动生命周期(Activity Lifecycle)

每一个Activity的状态是由它在活动栈中所处的位置所决定的,活动栈是当前所有正在运行的进程的后进先出的集合。当一个新的活动启动时,当前的前台屏幕就会移动到栈顶。如果用户使用Back(返回)按钮返回到刚才的活动,或者前台活动被关闭了,那么栈中的下一个活动就会移动栈顶,变为活动状态。

Activity的可能经历的四种状态:

活动状态(active or running):该活动位于栈顶,它是可见、具有焦点的前台活动。此时的活动可以接受用户的输入,Android不惜一切代价来保持它处于活动的状态,并根据需要来销毁下面部分的活动,以保证这个活动拥有它所需要的资源。当另一个活动变为活动状态时,此时的活动将被暂停。

暂停状态(paused):在某些情况下,活动是可见的,但是没有获得焦点,此时该活动处于暂停状态。当一个透明的或者非全屏的活动位于该活动之前时,就会处于此状态。当活动被暂停时,它仍然会被当作近似活动状态的状态,但是它不能接收用户的输入事件。然而,在极端情况下,Android会终止暂停状态,以便为其活动的状态释放资源。当一个活动变得完全不可见的时候,它变为停止状态。

停止状态(stopped):当一个活动完全不可见时,它处于停止状态。此时,该活动仍然存放在内存中,保持所有的状态和成员信息,然而当系统的其它地方要求使用内存时,它们就会成为被终止的首先对象。

注意:在一个活动被停止的时候,保存数据和当前的UI状态是很重要的。

一旦一个活动被退出或者关闭,它就会变为非活动状态。

非活动状态:当一个活动被终止之后,在启动之前它处于非活动状态。处于此状态的活动已经从活动中移除了,因此,在它们可以被显示和使用之前,需要重新启动。

 

  

 

 

 

 

  Activity生命周期

 

 

6、活动状态处理程序

方法

描述

Killable?

下一个

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.

Pre-HONEYCOMB

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值