The Android onCreate () method is introduced in detail _android

Source: Internet
Author: User

The OnCreate () method is one of the most common methods in the Android application, so what should we pay attention to when using the OnCreate () method?

First look at the Google Android developers online explanation:

OnCreate (Bundle) is where your initialize your activity. Most importantly, here you'll usually call Setcontentview (int) with a layout resource defining UI, and using your Ewbyid (int) to retrieve the widgets in that UI, that's you need to interact with programmatically.

Called 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.

can call finish () from within this function, in which case OnDestroy () 'll be immediately called without any of the R EST 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 does not, an exception would be thrown.

In other words, the onCreate () function is invoked at the time the activity is initialized, and normally we need to call the Setcontentview (int) function in OnCreate () to populate the screen UI, typically through Findviewbyid ( int) returns the ID of the view or component defined in the XML. Subclasses must call the parent class's OnCreate () method, or Super.oncreate (), if the OnCreate () method is overridden, otherwise an exception is thrown.

But what we have to be aware of is that we need to configure some of the necessary information in the OnCreate () function, but not everything can be done here. We know that the first function of an activity initiation call is OnCreate, which mainly does the necessary initialization when the activity is started, and after this function has been called, it does not mean that it has been started, or that it has jumped to the foreground. But it also requires a lot of work, we know: After OnCreate there are Onrestart () and OnStart (), and so on, actually onstart () the activity has not yet been fully activated, but only the foreground is visible until Onresume () After the call this oncreate finally started. In that case, any time-consuming action before an activity is actually started can lead to a slow start, especially if it takes a long time in OnCreate, which may lead to a very poor user experience.

Here's an example:

 protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stu
  b 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 the MTK//mcalloptionhandler = new Calloptionhandler (this);    
  Mcalloptionhandler = new Contactscalloptionhandler (This, new Contactscalloptionhandlerfactory ());
 
Don ' t consider getting no data, Ex:when starting up Updateenabledcard (); }

This is the oncreate of an activity of an app. In fact, this code is not a problem, but also seems to be relatively simple code. But there are a lot of dangerous code snippets: Whether it's dataload = new dataloading (); or mspeeddaildatamgr = new Speeddailmgr (this), more or Loadgripview (), or even updateenabledcard (); Such dangerous treatment should not be dealt with here. This includes loading database data, reading file information, reading SIM card information, these operations are likely to throw exceptions, and its operation is time-consuming is also uncertain! In the face of such a problem, I think should pay attention to the following aspects:

(1) Do as little as possible before the activity starts.

(2) for the layout is more complex, you can consider not a one-time full load, dynamic loading is a good way.

(3) for the timely need of data, loading up the time-consuming and dangerous, you must remember to open a thread to do these actions, do not remember to block the main thread (UI thread) anything.

(4) For special situations, activity initiation does require a lot of work, and you can consider loading a simple layout (or an activity) to transition.

(5) All the purpose is to let you start the component as soon as possible, rather than painting a good makeup, so that guests will not wait, the customer is God.

The above is the Android OnCreate () method of detailed introduction, follow-up to continue to supplement the relevant knowledge, thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.