Acitivity的生命週期



     Activity在Android是一個極其常用類,與用戶交互離不開它。

     我們先看一段Google對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{@link #setContentView}.  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{@link android.R.attr#windowIsFloating} set) or embedded inside of another activity (using{@link ActivityGroup}).

     上面一段主要就是說,Activity主要是用來創建一個與用戶交互的窗口,通過setContentView方法,可以將各種UI放在這個窗口上。Activity可以是全屏的窗口或者說是介面,也是可以懸浮的,也可以一個Activity嵌套包含在另一個Activity裏形成一個Activity組合(ActivityGroup)。這裏要說一下是,ActivityGroup,Google已建議在HONEYCOMB以後的開發中,用Fragment和FragmentManager來代替。
接下來,看下面一段註解:There are two methods almost all subclasses of Activity will implement:{@link #onCreate} is where you initialize your activity.  Most importantly, here you will usually call{@link #setContentView(int)} with a layout resource defining your UI, and using{@link #findViewById} to retrieve the widgets in that UI that you need to interact with programmatically.{@link #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{@link android.content.ContentProvider} holding the data). 上面說Activity的子類一般都會覆寫onCreate和onPause兩個方法。其中onCreate主要用來初始化Activity所需要的各個UI組件,onPause則可以在用戶離開介面的時候保存數據。

     下面我們主要看一下Activity的生命週期:

     1,Activity的整個生命週期。從onCreate開始初始化,到onDestroy結束釋放所有數據。 附上Google註解:The <b>entire lifetime</b> of an activity happens between the first call to{@link android.app.Activity#onCreate} through to a single final call to{@link android.app.Activity#onDestroy}.  An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy().  For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy().

     2,可視化的生命週期。這個生命週期發在onStart調用開始到onStop調用結束之間。而且在Activity的顯示和隱藏的切換過程中,這兩個方法可以被多次調用。附上Google註解:The <b>visible lifetime</b> of an activity happens between a call to{@link android.app.Activity#onStart} until a corresponding call to{@link android.app.Activity#onStop}.  During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user.  Between these two methods you can maintain resources that are needed to show the activity to the user.  For example, you can register a{@link android.content.BroadcastReceiver} in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying.  The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.

     3,前臺生命週期。這個生命週期出現在onResume和onPause方法調用之間。onResume時,Activity處於最前端,也是處於ActivityStack的頂部,用戶可以寫之交互。由於這兩個方法的切換比較頻繁,所經代碼要儘量是輕量的,能夠瞬時完成的,也就是說非耗時的。附上Google註解:The <b>foreground lifetime</b> of an activity happens between a call to{@link android.app.Activity#onResume} until a corresponding call to{@link android.app.Activity#onPause}.  During this time the activity is in front of all other activities and interacting with the user.  An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.

     究竟Activity在什麼時候保存各種狀態和數據,這裏也是有說明的,看下面一段:The entire lifecycle of an activity is defined by the following Activity methods.  All of these are hooks that you can override to do appropriate work when the activity changes state.  All activities will implement{@link android.app.Activity#onCreate} to do their initial setup; many will also implement{@link android.app.Activity#onPause} to commit changes to data and otherwise prepare to stop interacting with the user.  You should always call up to your superclass when implementing these methods.

     由此可以看出,Activity都是在onCreate中初始化數據,在onPause中保存數據,暫停與用戶的交互。上面說的following Activity methods 又是哪些方法呢,下面列出來:

  public class Activity extends ApplicationContext {
     protected void onCreate(Bundle savedInstanceState);
 
     protected void onStart();
     
     protected void onRestart();

     protected void onResume();
 
     protected void onPause();

     protected void onStop();
 
     protected void onDestroy();
  }

      以下一段代碼是註解中的例子,說的是在onPause方法中保存信息:

 public class CalendarActivity extends Activity {
    ...

    static final int DAY_VIEW_MODE = 0;
     static final int WEEK_VIEW_MODE = 1;

    private SharedPreferences mPrefs;
    private int mCurViewMode;

     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         SharedPreferences mPrefs = getSharedPreferences();
         mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
     }

     protected void onPause() {
        super.onPause();

        SharedPreferences.Editor ed = mPrefs.edit();
        ed.putInt("view_mode", mCurViewMode);
        ed.commit();     
		}
  }  
     關於Activity的其他種種,都可以去參照Google的Activity源碼以獲得更多的信息。





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