Explanation of onCreate () method for android development, androidoncreate

Source: Internet
Author: User

Explanation of onCreate () method for android development, androidoncreate

Here we only pay attention to one sentence: This is where you should do all of your normal static set up. We only focus on normal static,
Normal: General, General, and general.

Static: static, unchanged
The onCreate () method is one of the most common methods in android applications. So what issues should we pay attention to when using the onCreate () method?
The onCreate () function is called during activity initialization. Generally, we need to call the setContentView (int) function in onCreate () to fill the UI of the screen, the findViewById (int) is used to return the view or component ID defined in xml. When the subclass overrides the onCreate () method, it must call the onCreate () method of the parent class, that is, super. onCreate (). Otherwise, an exception is thrown.

However, we must note that we need to configure some necessary information in the onCreate () function, but not all things can be done here. We know that the first function called by an activity to start is onCreate, which mainly performs some necessary initialization work during the activity startup. After this function is called, this activity does not mean it has already started, or it has jumped to the foreground. It also requires a lot of other work. We know that after onCreate, there are onRestart () and onStart (). In fact, onStart () has been called and this activity has not been fully started yet, it is only visible at the front end, until onResume () is called, this onCreate is finally started. In this case, any time-consuming action before an activity is actually started will lead to slow activity startup, especially if it takes a long time in onCreate, which may lead to very poor user experience.

The following code is used in many security zones:

 1 protected void onCreate(Bundle savedInstanceState) { 2     // TODO Auto-generated method stub 3       4     super.onCreate(savedInstanceState); 5     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 6     mContext = this; 7     setContentView(R.layout.main); 8     dataLoad = new DataLoading(); 9     mScrollLayout = (ScrollLayout)findViewById(R.id.ScrollLayoutTest);10     btnExit = (ImageButton)findViewById(R.id.btn_exit);11     btnExit.setOnClickListener(btnExitClickListener);12     btnContacts = (ImageButton)findViewById(R.id.btn_contacts);13     btnContacts.setOnClickListener(btnContactsClickListener);14      15     mSpeedDailDataMgr = new SpeedDailMgr(this);16     loadGripView();17  18     //in MTK        19        //mCallOptionHandler = new CallOptionHandler(this);20        mCallOptionHandler = new ContactsCallOptionHandler(this,21                 new ContactsCallOptionHandlerFactory());        22     //don't consider getting no data, ex: when starting up23     updateEnabledCard();24  25 }


This is the onCreate method of an app Activity. In fact, this code is no problem, and it seems to be relatively simple code. However, there are a large number of dangerous code segments: whether it is dataLoad = new DataLoading (), mSpeedDailDataMgr = new SpeedDailMgr (this), or loadGripView (), or even updateEnabledCard (); such dangerous processing should not be handled here. These operations include loading database data, reading file information, and reading SIM card information. These operations may throw an exception, and the operation time is not certain! 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) When the layout is complex, consider not loading all at once. dynamic loading is a good solution.

(3) If loading time-consuming and abnormal data is required in a timely manner, remember to open a thread to do these operations. Do not block the main thread (UI thread) anything.

(4) In special cases, when Activity startup requires a lot of work, you can consider loading a simple layout (or Activity) for the transition ..

(5) All goals are to enable the components you want to start to play as soon as possible, rather than focusing on makeup. In this way, the guests will not wait, and the customers will be God.

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

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.