Android——onCreate( )方法详解(转)
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( )方法详解(转)的更多相关文章
- android开发之onCreate( )方法详解
这里我们只关注一句话:This is where you should do all of your normal static set up.其中我们只关注normal static,normal: ...
- Android GestureDetector方法详解
为了加强点击.拖动响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single ...
- Android ActionBar 关于tab的应用 以及 TabListener的方法详解
actionBar的tab标签应用以及TabListener的方法详解 package com.example.actionBarTest.actionBarTab; import android.a ...
- Android编程之LayoutInflater的inflate方法详解
LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到: public View onCreateView(LayoutInflater infl ...
- Android源码下载方法详解
转自:http://www.cnblogs.com/anakin/archive/2011/12/20/2295276.html Android源码下载方法详解 相信很多下载过内核的人都对这个很熟悉 ...
- Android Service生命周期 Service里面的onStartCommand()方法详解
在Demo上,Start一个Service之后,执行顺序:onCreate - > onStartCommand 然后关闭应用,会重新执行上面两步. 但是把代码拷贝到游戏工程发现,关闭游戏后,只 ...
- android emulator启动的两种方法详解
android emulator启动的两种方法详解 转https://blog.csdn.net/TTS_Kevin/article/details/7452237 对于android学习者,模 ...
- Android之canvas详解
首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, y ...
- 【转】Android Canvas绘图详解(图文)
转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html Android Canvas绘图详解(图文) 泡 ...
随机推荐
- php stdClass 的使用
原文:http://www.php.cn/php-weizijiaocheng-371767.html ------------------------------------------------ ...
- 浅析ActiveReport中数据下拉列表的交互性
虽说做Cognos已经很久了,Cognos的active report还很少开发过,于是便做了一些小的尝试,下面就以具体实例来分析一下在report studio的活动报表中数据下拉列表和列表报表以及 ...
- Cognos权限认证CJP方式之用户密码加密
在项目开发过程中,用户往往对系统的安全都有明确的要求,下面针对cognos门户认证用户密码如何加密来提供一个简单的wf 1Cognos权限认证方式:CJP 2Cognos用户数据库类型:Oracle ...
- python常用代码积累
一.文件操作 1.判断一个目录是否存在,若不存在则创建 if not os.path.isdir(new_path): os.makedirs(new_path) 2.新建一个文件 f=open(&q ...
- 关于listview,scrollview显示模糊边缘的设置
朋友们有时可能在开发中遇到这样的莫名其妙的问题,listview或scrollview滑动时上边和下边会出现两条模糊的边缘,有时会影响到我们app的视觉效果,我们怎么去掉这两条模糊的边缘呢?很简单,一 ...
- html5图表
http://www.html5tricks.com/tag/html5图表
- Android相机基础基于camera2API
前言 最近,在使用Android做一个照相机的开发.因为不能使用系统提供的相机应用,所以只能自己写一个.Android以前提供相机的api叫camera,不过在level 21被Google抛弃了.网 ...
- HTTP 头缓存Last-Modified,ETag,Expires
http://www.jdon.com/40381 Last-Modified和Expires针对浏览器,而ETag则与客户端无关,所以可适合REST架构中.两者都应用在浏览器端的区别是:Expire ...
- 安装TeX及中文支持
2014.7.19更新: 以下的笔记适用于在基于Ubuntu的发行版(比方LinuxMint)安装Texlive2013.2014: 第一步依据本机状况.可能不须要. Texlive2014已经能够下 ...
- NSURLConnection经常使用的代理方法
NSURLConnection的代理Protocol定义有三类:NSURLConnectionDelegate.NSURLConnectionDataDelegate和NSURLConnectionD ...