I recently read some developer APP development. Although all of them have achieved perfect functions, many of them are far-fetched! I think: as a qualified software engineer, the most important thing is not how much code you have written, but how much code you have studied! Therefore, I advocate more apps customized by the research system. Otherwise, there will be many security zones during development!
Today, let's talk about a ubiquitous function: onCreate ().. Let's take a look at how gogle explains him:
Called when the activity is first created. this is where you shoshould do all of your normal static set up: create views, bind data to lists, etc. this method also provides you with a Bundle containing the activity's previusly frozen state, if there was one.
Always followedonStart()
.
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.
Here we need to do the information that usually requires configuration, rather than doing everything here.We know that the first function of an activity startup callback is onCreate. This function is mainly used to initialize the activity. After the function is called, the activity does not mean that it has been started or jumped to the foreground. However, we still need a lot of other work. We know that onStart () and onStart () are called after onCreate. In fact, onStart () has been called and this activity has not been fully started yet, it is only visible at the front endonResume()
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 result in serious experience.
Let's first look at an instance:
@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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 upupdateEnabledCard();}
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 jump out of exceptions, 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, guests will not be able to wait. This Social customer is God.