The OnCreate () method analysis in the Android (Java) Learning Note 169:activity

Source: Internet
Author: User

The 1.onCreate () method is one of the most common methods of Android applications:

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 fully started, but also the foreground is visible, straight to 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.

2. code example:

protected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub              Super. OnCreate (savedinstancestate);  This. Requestwindowfeature (Window.feature_no_title); Mcontext= This;         Setcontentview (R.layout.main); Dataload=Newdataloading (); 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=NewSpeeddailmgr ( This);          Loadgripview (); //In MTK//Mcalloptionhandler = new Calloptionhandler (this);Mcalloptionhandler =NewContactscalloptionhandler ( This,                Newcontactscalloptionhandlerfactory ()); //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 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, activity startup does require a lot of work, you might 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 .

The OnCreate () method analysis in the Android (Java) Learning Note 169:activity

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.