天天看點

android開發之onCreate( )方法詳解

     onCreate( )方法是android應用程式中最常見的方法之一,那麼,我們在使用onCreate()方法的時候應該注意哪些問題呢?

    先看看Google Android Developers官網上的解釋:

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.          

   Called when the activity is starting. This is where most initialization should go: calling 

setContentView(int)

 to inflate the activity's UI, using 

findViewById(int)

 to programmatically interact with widgets in the UI, calling 

managedQuery(android.net.Uri, String[], String, String[], String)

 to retrieve cursors for data being displayed, etc.

   You can call 

finish()

 from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (

onStart()

,

onResume()

onPause()

, etc) executing.

   Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

   翻譯過來就是說,onCreate()函數是在activity初始化的時候調用的,通常情況下,我們需要在onCreate()中調用setContentView(int)函數填充螢幕的UI,一般通過findViewById(int)傳回xml中定義的視圖或元件的ID。子類在重寫onCreate()方法的時候必須調用父類的onCreate()方法,即super.onCreate(),否則會抛出異常。

   但是,我們必須要注意的是,在onCreate()函數裡我們需要配置一些必要的資訊,但是并不是所有的事情都能在這裡做。我們知道,一個activity啟動調用的第一個函數就是onCreate,它主要做這個activity啟動時一些必要的初始化工作,這個函數調用完後,這個activity并不是說就已經啟動了,或者是跳到前台了。而是還需要其他的大量工作,我們知道:onCreate之後還有onRestart()和onStart()等,實際上onStart()調用完畢了這個activity還沒有完全啟動,也隻是前台可見,直到 

onResume()

 調用後這個onCreate才算終于啟動。既然這樣,那麼在一個activity真正啟動之前任何相當耗時的動作都會導緻activity啟動緩慢,特别是在onCreate裡面耗時長的話可能導緻極差的使用者體驗。

   下面來看一個例子:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
     
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mContext = this;
    setContentView(R.layout.main);
    dataLoad = new DataLoading();
    mScrollLayout = (ScrollLayout)findViewById(R.id.ScrollLayoutTest);
    btnExit = (ImageButton)findViewById(R.id.btn_exit);
    btnExit.setOnClickListener(btnExitClickListener);
    btnContacts = (ImageButton)findViewById(R.id.btn_contacts);
    btnContacts.setOnClickListener(btnContactsClickListener);
     
    mSpeedDailDataMgr = new SpeedDailMgr(this);
    loadGripView();
 
    //in MTK        
       //mCallOptionHandler = new CallOptionHandler(this);
       mCallOptionHandler = new ContactsCallOptionHandler(this,
                new ContactsCallOptionHandlerFactory());        
    //don't consider getting no data, ex: when starting up
    updateEnabledCard();
 
}
           

這是一個APP的一個Activity的onCreate的寫法。其實這段代碼沒有什麼問題,而且看起來也是比較簡單的代碼。不過裡面大量危險的代碼段:不管是dataLoad = new DataLoading(); 還是 mSpeedDailDataMgr = new SpeedDailMgr(this);更或者是loadGripView();甚至updateEnabledCard();這麼危險的處理都是不應該在這裡來處理的。這裡包含了加載資料庫資料、讀取檔案資訊、讀取SIM卡資訊,這些操作都是有可能抛出異常的,而且其操作耗時也是不确定的!對于面對這樣問題,我覺得應該注意下面幾個方面:

(1)在Activity啟動前,盡量少做。

(2)對于布局比較複雜的時候,可以考慮不要一次性全部加載上,動态加載是一個好的辦法。

(3)對于及時需要的資料,加載起來耗時的又有異常危險的,一定記得開辟一個線程來做這些動作,千萬記得不要做阻塞主線程(UI線程)的任何事情。

(4)對于特殊情況下,Activity啟動确實需要大量工作時候,可以考慮先加載一個簡單的布局(或是Activity)來過渡.。

(5)所有的目的都是讓你要啟動的元件盡快上場,而不是以畫好妝為主,這樣的話客人會等不及的,顧客就是上帝。

參考文章:http://www.2cto.com/kf/201403/285613.html

繼續閱讀