Android--oncreate () method detailed

Source: Internet
Author: User

OnCreate () Method of Android development

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:

is onCreate(Bundle) where you initialize your activity. Most importantly, where you'll usually call setContentView(int) with a layout resource defining your UI, and using to findViewById(int) retrieve th e widgets in that UI so 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 to findViewById(int) programmatically I Nteract with widgets in the UI, calling to managedQuery(android.net.Uri, String[], String, String[], String) retrieve cursors for data being displayed, etc.

You can call from finish() within this function, in which case OnDestroy () 'll 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.

The onCreate () function is called when activity is initialized, and typically we need to call the Setcontentview (int) function in OnCreate () to populate the UI of the screen. The ID of the view or component defined in the XML is typically returned by Findviewbyid (int). Subclasses must call the parent class's OnCreate () method When overriding the OnCreate () method, or Super.oncreate (), or an exception will be thrown.

However, we must note that in the OnCreate () function we need to configure some necessary information, but not all things can be done here. As 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() After the call 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 (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 (); }

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

Android--oncreate () method (GO)

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.