OnCreate () Method of Android development

Source: Internet
Author: User

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

First look at Google Android developers online explanation:

onCreate(Bundle)is where you initialize your activity. Most importantly, here you'll usually callsetContentView(int)with a layout resource defining your UI, and usingfindViewById(int)To retrieve, the widgets in that UI, the need to interact with programmatically.          

   Called when the Activity is starting. This is the 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 < Code style= "Color:rgb (0,102,0); Line-height:14px ">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 () would 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 does not, an exception would be thrown.

    translates to say that the onCreate () function is called when activity is initialized, and usually we need to onCreate () calls the Setcontentview (int) function to populate the UI of the screen, typically by Findviewbyid (int) to return the ID of the view or component defined in the XML. Subclass in rewrite oncreate () method must call the parent class's OnCreate () method, or Super.oncreate (), or an exception will be thrown.

    However, we must note that the Span style= "Color:rgb (51,51,51); line-height:28px; Text-indent:28px "> in the OnCreate () function we need to configure some of the necessary information, but not all things can be done here. we know, The first function of an activity start call is OnCreate, which mainly does some necessary initialization work when the activity starts, and after this function is called, the activity does not say that it has started or jumps to the foreground. But also need a lot of other work, we know: OnCreate after the Onrestart () and OnStart (), and so on, in fact, the OnStart () call completed the activity has not been fully started, but also the foreground is visible until   onresume ()   call after this oncreate is finally started. In that case, any time-consuming action before an activity actually starts can cause the activity to start slowly, especially if it takes a long time in the OnCreate to lead to a very poor user experience.

Let's look at an example:

protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method stub super.oncreate (savedins    Tancestate);    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 (); }

This is an activity of an app's OnCreate notation. In fact, this code is not a problem, and it seems to be a relatively simple code. However, there are a lot of dangerous code snippets: either dataload = new dataloading (); or mspeeddaildatamgr = new Speeddailmgr (this), or loadgripview () or even updateenabledcard (), so dangerous handling should not be dealt with here. This includes loading database data, reading file information, reading SIM card information, these operations are likely to throw an exception, and its operation time is uncertain! In the face of such problems, I think we 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 all-in-one loading, dynamic loading is a good way.

(3) For timely data, loading time is also very dangerous, you must remember to open up a thread to do these actions, do not remember to do not block the main thread (UI thread) anything.

(4) For special cases, the activity startup does require a lot of work, you can consider loading a simple layout (or activity) to transition.

(5) All the goals are to get the components you want to start up as soon as possible, rather than to paint your makeup, so guests can't wait and the customer is God.

Reference article: http://www.2cto.com/kf/201403/285613.html


OnCreate () Method of Android development

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.