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?
Let’s take a look at the explanation on the official website of Google Android Developers:
onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that 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 findViewById(int) to programmatically interact with widgets in the UI, calling managedQuery(, String[], String, String[], String) to retrieve cursors for data being displayed, etc.
You can call finish() from within this function, in which case onDestroy() will 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 do not, an exception will be thrown.
Translated that means, the onCreate() function is called when the activity is initialized. Generally, we need to call the setContentView(int) function in onCreate() to fill the screen UI. Generally, we return the ID of the view or component defined in xml through findViewById(int). When the subclass overrides the onCreate() method, it must call the onCreate() method of the parent class, that is, (), otherwise an exception will be thrown.
However, we must note that in the onCreate() function we need to configure some necessary information, but not everything can be done here. We know that the first function called by an activity is onCreate, which mainly does some necessary initialization work when the activity is started. After the function is called, the activity does not mean that it has been started or jumped to the foreground. Instead, there are also a lot of other work. We know that there are onRestart() and onStart() after onCreate. In fact, the activity has not been fully started after the call to onStart(), and it is only visible to the foreground. It is not until the onResume() call that the onCreate is finally started. In this case, any time-consuming action before an activity is actually started will cause the activity to start slowly, especially if it takes a long time in onCreate, it may lead to a very poor user experience.
Let’s take a look at an example:
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub (savedInstanceState); (Window.FEATURE_NO_TITLE); mContext = this; setContentView(); dataLoad = new DataLoading(); mScrollLayout = (ScrollLayout)findViewById(); btnExit = (ImageButton)findViewById(.btn_exit); (btnExitClickListener); btnContacts = (ImageButton)findViewById(.btn_contacts); (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 the writing method of onCreate of an Activity of an APP. Actually, there is nothing wrong with this code, and it looks like a relatively simple code. However, there are a lot of dangerous code snippets in it: such a dangerous processing should not be handled here, whether it is dataLoad = new DataLoading(); or mSpeedDailDataMgr = new SpeedDailMgr(this); or even loadGripView(); or even updateEnabledCard(); This includes loading database data, reading file information, and reading SIM card information. These operations may throw exceptions, and their operation time is uncertain! When facing such a problem, I think the following aspects should be paid attention to:
(1) Do as little as possible before the Activity starts.
(2) When the layout is relatively complicated, you can consider not loading it all at once. Dynamic loading is a good way.
(3) For data required in time, if it takes time to load and is extremely dangerous, remember to open a thread to do these actions. Remember not to do anything that blocks the main thread (UI thread).
(4) In special cases, when Activity startup does require a lot of work, you can consider loading a simple layout (or Activity) first to transition.
(5) All the purpose is to let the components you want to start appear as soon as possible, rather than mainly using good makeup, so that customers will not be able to wait, and the customer is God.
The above is a detailed introduction to the Android OnCreate() method. We will continue to add relevant knowledge in the future. Thank you for your support for this site!