欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android onCreate( )方法詳細(xì)介紹

 更新時(shí)間:2016年09月06日 15:59:26   投稿:lqh  
本文主要介紹Android onCreate( )方法,做Android應(yīng)用的朋友對(duì)onCreate()的方法并不陌生,在開發(fā)應(yīng)用的時(shí)候大家應(yīng)該注意什么呢,這里給大家詳細(xì)說(shuō)明

onCreate( )方法是android應(yīng)用程序中最常見的方法之一,那么,我們?cè)谑褂胦nCreate()方法的時(shí)候應(yīng)該注意哪些問(wèn)題呢?

    先看看Google Android Developers官網(wǎng)上的解釋:

    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.

   翻譯過(guò)來(lái)就是說(shuō),onCreate()函數(shù)是在activity初始化的時(shí)候調(diào)用的,通常情況下,我們需要在onCreate()中調(diào)用setContentView(int)函數(shù)填充屏幕的UI,一般通過(guò)findViewById(int)返回xml中定義的視圖或組件的ID。子類在重寫onCreate()方法的時(shí)候必須調(diào)用父類的onCreate()方法,即super.onCreate(),否則會(huì)拋出異常。

   但是,我們必須要注意的是,在onCreate()函數(shù)里我們需要配置一些必要的信息,但是并不是所有的事情都能在這里做。我們知道,一個(gè)activity啟動(dòng)調(diào)用的第一個(gè)函數(shù)就是onCreate,它主要做這個(gè)activity啟動(dòng)時(shí)一些必要的初始化工作,這個(gè)函數(shù)調(diào)用完后,這個(gè)activity并不是說(shuō)就已經(jīng)啟動(dòng)了,或者是跳到前臺(tái)了。而是還需要其他的大量工作,我們知道:onCreate之后還有onRestart()和onStart()等,實(shí)際上onStart()調(diào)用完畢了這個(gè)activity還沒有完全啟動(dòng),也只是前臺(tái)可見,直到 onResume() 調(diào)用后這個(gè)onCreate才算終于啟動(dòng)。既然這樣,那么在一個(gè)activity真正啟動(dòng)之前任何相當(dāng)耗時(shí)的動(dòng)作都會(huì)導(dǎo)致activity啟動(dòng)緩慢,特別是在onCreate里面耗時(shí)長(zhǎng)的話可能導(dǎo)致極差的用戶體驗(yàn)。

   下面來(lái)看一個(gè)例子:

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();
 
}

     這是一個(gè)APP的一個(gè)Activity的onCreate的寫法。其實(shí)這段代碼沒有什么問(wèn)題,而且看起來(lái)也是比較簡(jiǎn)單的代碼。不過(guò)里面大量危險(xiǎn)的代碼段:不管是dataLoad = new DataLoading(); 還是 mSpeedDailDataMgr = new SpeedDailMgr(this);更或者是loadGripView();甚至updateEnabledCard();這么危險(xiǎn)的處理都是不應(yīng)該在這里來(lái)處理的。這里包含了加載數(shù)據(jù)庫(kù)數(shù)據(jù)、讀取文件信息、讀取SIM卡信息,這些操作都是有可能拋出異常的,而且其操作耗時(shí)也是不確定的!對(duì)于面對(duì)這樣問(wèn)題,我覺得應(yīng)該注意下面幾個(gè)方面:

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

(2)對(duì)于布局比較復(fù)雜的時(shí)候,可以考慮不要一次性全部加載上,動(dòng)態(tài)加載是一個(gè)好的辦法。

(3)對(duì)于及時(shí)需要的數(shù)據(jù),加載起來(lái)耗時(shí)的又有異常危險(xiǎn)的,一定記得開辟一個(gè)線程來(lái)做這些動(dòng)作,千萬(wàn)記得不要做阻塞主線程(UI線程)的任何事情。

(4)對(duì)于特殊情況下,Activity啟動(dòng)確實(shí)需要大量工作時(shí)候,可以考慮先加載一個(gè)簡(jiǎn)單的布局(或是Activity)來(lái)過(guò)渡.。

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

以上就是對(duì)Android OnCreate()方法的詳細(xì)介紹,后續(xù)繼續(xù)補(bǔ)充相關(guān)知識(shí),謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論