onCreate

来源:互联网 发布:微博如何绑定淘宝 编辑:程序博客网 时间:2024/06/03 02:28

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(),否则会抛出异常。

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

这里我们只关注一句话:This is where you should do all of your normal static set up。其中我们只关注normal static,
normal:常规的、通常的、一般的 。
static:静态的,不变的。
说的是:在这里我们需要做通常需要配置的信息,而不是真的所有的事情都在这里做。我们知道,一个activity启动回调的第一个函数就是onCreate。这个函数主要做这个activity启动的一些必要的初始化的工作,这个函数调用完后,这个activity并不是说就已经启动了,或者是跳到前台了。而是还需要其他的大量工作,我们知道:onCreate之后调用了还有onRestart()和onStart()等,实际上onStart()调用完毕了这个activity还没有完全启动,也只是前台可见,直到 onResume() 后这个onCreate才算终于启动。既然这样,那么在一个activity真正启动之前任何相当耗时的动作都会导致activity启动缓慢,特别是在onCreate里面耗时长的话可能照成严重的体验效果。
我们来先看一个实例:

@Overrideprotected 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

0 0
原创粉丝点击