Detailed introduction to Android oncreate() method

Oncreate () method is one of the most common methods in Android applications, so what should we pay attention to when using 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(android.net.Uri,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.

In other words, the onCreate () function is invoked when activity is initialized. In general, we need to call setContentView (int) function in onCreate () to fill the UI of the screen, and generally return to ID or the component defined in XML by findViewById (int). When overriding the oncreate () method, a subclass must call the oncreate () method of the parent class, that is, super Oncreate(), otherwise an exception will be thrown.

However, we must note that we need to configure some necessary information in the oncreate () function, but not everything can be done here. As we know, oncreate is the first function called by an activity startup. It 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 jump to the foreground. We know that oncreate is followed by onrestart() and onstart(). In fact, after the call of onstart(), the activity has not been fully started, which is only visible in the foreground. Oncreate is not finally started until onresume() is called. 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, which may lead to a very poor user experience.

Here is an example:

This is the description of oncreate of an activity of an app. In fact, there is no problem with this code, and it seems to be relatively simple code. However, there are a lot of dangerous code snippets: whether dataload = new dataloading(); Or msdaildatamgr = new speeddailmgr (this); Or loadgripview(); Even updateenabledcard(); Such dangerous treatment should not be handled here. This includes loading database data, reading file information and reading SIM card information. These operations may throw exceptions, and the 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) When the layout is complex, you can consider not loading all at once. Dynamic loading is a good method.

(3) For timely data that is time-consuming and extremely dangerous to load, you must remember to open a thread to do these actions, and never 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) to transition.

(5) All the purpose is to let the components you want to start play as soon as possible, rather than painting good makeup. In this way, the guests will not wait. The customer is God.

The above is a detailed introduction to the Android oncreate() method. We will continue to supplement relevant knowledge in the future. Thank you for your support for this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>