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

Android——onCreate( )方法详解(转)的更多相关文章

  1. android开发之onCreate( )方法详解

    这里我们只关注一句话:This is where you should do all of your normal static set up.其中我们只关注normal static,normal: ...

  2. Android GestureDetector方法详解

    为了加强点击.拖动响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single ...

  3. Android ActionBar 关于tab的应用 以及 TabListener的方法详解

    actionBar的tab标签应用以及TabListener的方法详解 package com.example.actionBarTest.actionBarTab; import android.a ...

  4. Android编程之LayoutInflater的inflate方法详解

    LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到: public View onCreateView(LayoutInflater infl ...

  5. Android源码下载方法详解

    转自:http://www.cnblogs.com/anakin/archive/2011/12/20/2295276.html Android源码下载方法详解 相信很多下载过内核的人都对这个很熟悉 ...

  6. Android Service生命周期 Service里面的onStartCommand()方法详解

    在Demo上,Start一个Service之后,执行顺序:onCreate - > onStartCommand 然后关闭应用,会重新执行上面两步. 但是把代码拷贝到游戏工程发现,关闭游戏后,只 ...

  7. android emulator启动的两种方法详解

    android emulator启动的两种方法详解    转https://blog.csdn.net/TTS_Kevin/article/details/7452237 对于android学习者,模 ...

  8. Android之canvas详解

    首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, y ...

  9. 【转】Android Canvas绘图详解(图文)

    转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html Android Canvas绘图详解(图文) 泡 ...

随机推荐

  1. 【Linux】shell字符串分割、数组访问、条件判断

    参考资料: shell字符串分割再循环:http://www.shangxueba.com/jingyan/1633455.html linux shell中 if else以及大于.小于.等于逻辑表 ...

  2. idea maven 报-source 1.5 中不支持 diamond 运算符

    需要修改 project setting 中的

  3. python 相似语句匹配(非机器学习)

    #coding=utf-8 import xlrd import distance from sklearn.feature_extraction.text import CountVectorize ...

  4. CSS3-border-radius 属性

    向 div 元素添加圆角边框: div { border:2px solid; border-radius:25px; } IE9+.Firefox 4+.Chrome.Safari 5+ 以及 Op ...

  5. ssh 的安装

    新安装的ubuntu 虚拟机,没有ssh时(ssh 连接不上),时ssh服务没装. 安装openssh-server,就可以. ------------------------------------ ...

  6. [Functional Programming] Function signature

    It is really important to understand function signature in functional programming. The the code exam ...

  7. 公共DNS服务

    一: 谷歌的 8.8.8.8 8.8.4.4 国内的两组 114.114.114.114 114.114.115.115

  8. Java浮点运算-BigDecimal

    package com.hsun.test; import static java.lang.System.out; import java.math.BigDecimal; public class ...

  9. 判断IE浏览器版本的精简脚本

    IE浏览器不管是什么版本,总是跟Web标准有些不太兼容.对于代码工作者来说,自然是苦不堪言,为了考虑IE的兼容问题,不管是写 CSS 还是 JS,往往都要对 IE 特别对待,这就少不了做些判断.本文不 ...

  10. 保护心灵窗口——防蓝光软件f.lux

    一款根据时间变化来自动改变屏幕色温的软件.让你在深夜也能感受到太阳的温暖,顺便还有助于睡眠.相较于花大价钱购置防蓝光屏或者防蓝光膜,这款软件还是excellent的 首先,概念科普(蓝光的危害就略略略 ...