您的位置:首页 > 移动开发 > Android开发

Android------------>Activity 生命周期

2016-07-03 19:47 375 查看

Activity:

一个活动是一个单一的,集中的事情,用户可以做。几乎所有的活动都与用户进行交互,所以活动类负责创建,你可以把你的UI setContentView你窗口(视图)。虽然活动往往呈现给用户的全屏窗口,也可以用在其他方面:如浮动窗口(通过一个windowisfloating设置主题)或嵌入在另一个活动(使用为)。有两种方法,几乎所有的子活动将实现onCreate(束)是你初始化你的活动。最重要的是,你通常会调用setContentView(int)和布局的资源定义你的UI,并使用findViewById(int)检索在UI中你需要与编程。onpause()就是你处理用户离开你的活动。最重要的是,用户所做的任何更改应在这一点上所做的承诺(通常ContentProvider的数据举行)。

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 withsetContentView(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). There
are two methods almost all subclasses of Activity will implement:

onCreate(Bundle)
 is
where you initialize your activity. Most importantly, here you will usually call 
setContentView(int)
 with
a layout resource defining your UI, and using 
findViewById(int)
 to
retrieve the widgets in that UI that you need to interact with programmatically.

onPause()
 is
where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be committed (usually to the 
ContentProvider
 holding
the data).


根据API中Activity生命周期:

在系统中的活动被管理作为一个活动栈。当一个新的活动开始时,它被放置在堆栈的顶部,并成为运行活动-以前的活动总是保持在它下面的堆栈,并不会再次到前台,直到新的活动退出。一个活动基本上是四个状态:如果在屏幕前的一个活动(在堆栈的顶部),它是主动或运行。如果一个活动失去了焦点,但仍然可见(也就是说,一个新的非完整大小或透明的活动集中在你的活动之上),它被暂停了。一个暂停的活动是完全活着(它保持所有的状态和成员信息,并保持连接到窗口管理器),但可以杀死系统在极端低的内存情况下。如果一个活动被另一个活动完全遮蔽,它就停止了。它仍然保留了所有的状态和成员信息,但是,它不再是可见的用户,所以它的窗口是隐藏的,它会经常被系统杀死时,内存是需要在其他地方。如果一个活动被暂停或停止,系统可以从内存中停止活动,或者要求它完成,或简单地杀死它的过程。当它再次显示给用户时,它必须完全重新启动并恢复到它以前的状态。下面的图显示了一个活动的重要状态路径。矩形矩形表示当活动在状态之间移动时,可以实现执行操作的回调方法。彩色的椭圆形是主要国家的活动可以。

Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come
to the foreground again until the new activity exits.

An activity has essentially four states:

If an activity in the foreground of the screen (at the top of the stack), it is active or running.

If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information
and remains attached to the window manager), but can be killed by the system in extreme low memory situations.

If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system
when memory is needed elsewhere.

If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

The following diagram shows the important state paths of an Activity. The square rectangles represent callback methods you can implement to perform operations when the Activity moves between states. The colored ovals are major states the Activity can be
in.



周期解释:

有三个关键环你可以在你感兴趣的活动:在监测活动的整个一生从第一次调用onCreate(Bundle)通过一个单一的最后一次通话ondestroy()。活动将尽”的设置在oncreate()全球”的状态,并在ondestroy()释放所有剩余资源。例如,如果一个线程在后台从网络上下载数据,它可以创建线程oncreate()然后停止线程ondestroy()。一个活动的可视电话onstart()一生直到相应的调用onstop()之间。在此期间,用户可以看到屏幕上的活动,虽然它可能不在前台,并与用户交互。在这两种方法之间,您可以维护所需的资源,以显示该活动给用户。例如,你可以在onstart()登记一个BroadcastReceiver来影响你的用户界面的变化监测,并注销在onstop()当用户不再看你的显示。的onstart()和onstop()方法可以多次调用,为活动变得可见和隐藏的用户。一个活动的前景一生打电话onresume()直到相应的调用onpause()之间。在此期间,活动是在所有其他活动的前面,并与用户交互。一个活动可以频繁地在恢复和暂停状态之间进行,例如当设备进入休眠状态时,当一个活动结果被传递时,一个新的意图被传递-所以这些方法中的代码应该是相当轻量级的。

1.启动Activity:系统会先调用onCreate方法,然后调用onStart方法,最后调用onResume,Activity进入运行状态。

2.当前Activity被其他Activity覆盖其上或被锁屏:系统会调用onPause方法,暂停当前Activity的执行。

3.当前Activity由被覆盖状态回到前台或解锁屏:系统会调用onResume方法,再次进入运行状态。

4.当前Activity转到新的Activity界面或按Home键回到主屏,自身退居后台:系统会先调用onPause方法,然后调用onStop方法,进入停滞状态。

5.用户后退回到此Activity:系统会先调用onRestart方法,然后调用onStart方法,最后调用onResume方法,再次进入运行状态。

6.当前Activity处于被覆盖状态或者后台不可见状态,即第2步和第4步,系统内存不足,杀死当前Activity,而后用户退回当前Activity:再次调用onCreate方法、onStart方法、onResume方法,进入运行状态。

7.用户退出当前Activity:系统先调用onPause方法,然后调用onStop方法,最后调用onDestory方法,结束当前Activity。

当一个Android 程序运行时会执行:



进入另一个Activity时会执行:



返回上一次的Activity是会执行:



当退出程序时:



退出程序再重新进入程序:






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: