Android深入瞭解Activity

一、Activity簡述

1、概念引入

上圖爲Activity類圖結構,Activity作爲Android的四大組件之一,Activity在Android系統中是以界面的形式進行體現。其中Activity實現瞭如Window.Callback, KeyEvent.Callback等接口用於與用戶進行交互。

2、源碼釋義

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). 
幾乎所有界面都可以與用戶交互,所以Activity類負責爲用戶創建一個窗口,你可以在其中使用setContentView(View)放置UI。

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

雖然界面通常以全屏窗口的形式呈現給用戶,但它們也可以以其他方式使用:作爲浮動窗口(通過具有windowIsFloating集合的主題)或嵌入另一個活動(使用ActivityGroup)內部。

There are two methods almost all subclasses of Activity will implement:

幾乎所有的Activity子類都會實現兩種方法:

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.
onCreate(Bundle)是初始化界面的地方。最重要的是,在這裏你通常會調用setContentView(int)和一個定義你的UI的佈局資源,並且使用findViewById(int)來檢索你需要以編程方式進行交互的那個UI中控件。

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).
onPause()是你對於用戶離開界面的處理。最重要的是,此時用戶所做的任何更改都應該提交(通常發送給持有數據的ContentProvider。

To be of use with Context.startActivity(), all activity classes must have a corresponding declaration in their package's AndroidManifest.xml.
要使用Context.startActivity(),所有界面類必須在其包下的AndroidManifest.xml中具有相應的聲明。

 

二、Activity的生命週期

1、Activity的生命週期圖

2、生命週期詳解

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().
此方法還會爲你提供一個包含Activity先前保存頁面狀態的Bundle對象(如果有的話)。總是跟着onStart()方法。

 2.2、OnRestart()方法

Called after your activity has been stopped, prior to it being started again.
在你的界面停止後調用,然後再次啓動。

Always followed by onStart()

總是跟着onStart()

 2.3、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.

如果活動進入前臺,則跟隨執行onResume()方法,如果隱藏,則執行onStop()。
此時,Activity實際上就已經可見了,但是還沒有出現在前臺,無法和用戶進行交互。

 2.4、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()方法,如果隱藏,則執行onStop()方法。
此時,Activity已經可見,並且出現在前臺並開始活動。要注意的是onStart()被執行時Activity顯示在後臺,只有當OnResume()被執行時Activity才顯示到前臺。

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

onPause()方法在系統即將開始顯示之前的界面時調用。 這通常用於將未保存的更改進行保存,並保存爲持久化數據,並且會執行例如停止動畫和其他可能消耗CPU的內容等。此方法的實現必須非常快速,因爲在此方法返回之前,下一個界面不會顯示。

Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.

如果界面返回到前臺,則跟隨onResume();如果對用戶不可見,則使用onStop()

onPause()方法執行後,Activity界面切換爲後臺程序

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

onStop()方法會在當界面對用戶不再可見時調用,因爲另一項界面正在顯示並要去覆蓋這個界面。 這可能是因爲一項新的界面正在開始,其他的界面會被顯示爲前臺界面,或者這個界面正在被摧毀。

Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.

如果這個界面快速的回顯與用戶交互緊接着onRestart()會被執行,否則,這個界面回銷毀onDestroy()方法會被執行。

 2.7、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.)

在你的界面被銷燬前被最後調用的方法。 這可能是因爲界面正在結束(有人在其中調用finish()方法因爲系統會暫時銷燬此Activity界面的實例以節省空間,你可以使用isFinishing()方法區分內存空間是否節省資源的這兩種情況)。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章