当前位置: 首页 > 编程笔记 >

Android onCreate( )方法详细介绍

宗政燕七
2023-03-14
本文向大家介绍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)所有的目的都是让你要启动的组件尽快上场,而不是以画好妆为主,这样的话客人会等不及的,顾客就是上帝。

以上就是对Android OnCreate()方法的详细介绍,后续继续补充相关知识,谢谢大家对本站的支持!

 类似资料:
  • 本文向大家介绍Bootstrap table使用方法详细介绍,包括了Bootstrap table使用方法详细介绍的使用技巧和注意事项,需要的朋友参考一下 bootstrap-table是一个非常好用的表格插件,提供了很多工具及分页、搜索等功能。 首先我们需要下面几个文件, 中文包好像可以防止某些bug 如果你单独引入这些文件而导致样式出错,请下载完整的bootstrap-table 下面是boo

  • 本文向大家介绍IOS Bundle详细介绍及使用方法,包括了IOS Bundle详细介绍及使用方法的使用技巧和注意事项,需要的朋友参考一下 什么是Bundle? A bundle is a directory with a standardized hierarchical structure that holds executable code and the resources used by

  • 本文向大家介绍Android Service中方法使用详细介绍,包括了Android Service中方法使用详细介绍的使用技巧和注意事项,需要的朋友参考一下  service作为四大组件值得我们的更多的关注 在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务。例如,一个从service播放音乐的音乐播放器,应被设置为前台运行,因为用户会明确地注意

  • 本文向大家介绍重写hashCode()和equals()方法详细介绍,包括了重写hashCode()和equals()方法详细介绍的使用技巧和注意事项,需要的朋友参考一下 hashCode()和equals()方法可以说是Java完全面向对象的一大特色.它为我们的编程提供便利的同时也带来了很多危险.这篇文章我们就讨论一下如何正解理解和使用这2个方法. 如何重写equals()方法 如果你决定要重写

  • 本文向大家介绍XenServer 日志清理方法详细介绍,包括了XenServer 日志清理方法详细介绍的使用技巧和注意事项,需要的朋友参考一下  XenServer日志清理 相信很多人被Xenserver日志填满磁盘空间,导致机器最终挂掉的问题所困扰,我遇见了2次(有一次是pool master挂掉,200号人1个小时无法办公)。令人很是头疼。 服务器使用时间长了,XenServer产生了很多日志

  • 本文向大家介绍Android  ADB详细介绍及用法,包括了Android  ADB详细介绍及用法的使用技巧和注意事项,需要的朋友参考一下 Android ADB 用法 adb  全称是 Android Debug Bridge, 就是起到调试桥的作用。 用来操作android设备的 阅读目录 adb 有什么用 adb 下载 adb devices adb install  (安装软件) adb