The OnCreate of Android development

Source: Internet
Author: User

Recently saw some developers do the app development, although all achieve a relatively perfect function, but many places is very far-fetched! I think: As a qualified software engineer, the most important thing is not how much code you write, but how much code you have studied! Therefore, I advocate more research system originally customized some of the app, or the development of their own when there will be a lot of mined areas!

Today, let's start by saying a ubiquitous function: OnCreate (). Let's see how Gogle explains him:

Called when the activity is first created. This is the where you should does all the your normal static set up:create views, bind data to lists, etc. This method also provides your with a Bundle containing the activity's previously frozen state, if there is one.

Always followed by onStart() .

Here we only pay attention to one sentence: this is where you should does all of the your normal static set up. Where we only focus on normal static,

Normal: Normal, usual, general.

Static: Stationary, unchanging.

Here's the point: here we need to do the information that usually needs to be configured, instead of really doing all the things here. we know that the first function of an activity startup callback is OnCreate. This function mainly does the activity initiated some necessary initialization of the work, after the function call, the activity is not said to have started, or jumped to the foreground. But we also need a lot of other work, we know: OnCreate after the call Onrestart () and OnStart () and so on, in fact, the OnStart () call completed the activity has not fully started, but also only the foreground is visible until onResume() After this oncreate was finally started. In that case, any time-consuming action before an activity actually starts will cause the activity to start slowly, especially if it takes a long time in the oncreate to be a serious experience.

Let's look at an example first:

@Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); this.requestwindowfeature (window.feature_no_title); mcontext = This;setcontentview ( R.layout.main);d ataload = 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 Upupdateenabledcard ();}

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 the database data, reading the file information, reading the SIM card information, these operations are likely to jump out of the 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 draw your makeup, so guests can't wait, the social customer is God.

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

The OnCreate of Android development

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.