Application initialization of Android system programming introduction series

In the previous article, we learned that when the Android system starts the application, it will first load a series of information in the androidmanifest.xml manifest file. If the name attribute value in the < Application > < / Application > tag is not specified in the manifest file, android.app.application will be used as the application loading class by default, Its lifecycle is consistent with the application lifecycle. So what kind of life cycle does the application have, and what call methods can be used in different life cycles? This chapter will introduce in detail.

First of all, an application has only one android.app.application class corresponding to it. If you want to use a custom application in the manifest file, it must also be a subclass inherited from android.app.application. The instance of application is created prior to any other class in the application when the Android system starts the application.

Since applicaiton is a subclass of android.content.contentwrapper, after the application is instantiated and created, the Android system will give priority to calling the attachbasecontext (context base) method of this class, so as to bind the context base parameter to the application.

Android.content.context here is the running environment created by the Android system for the application. Through this class, you can access the application's resource file, send intention, receive broadcast and other application level operations.

The contentwrapper class is defined in the Android system as the parent class, which is inherited by the component that needs to bind the context environment. Theoretically, any subclass inherited from contentwrapper can overload its attachbasecontext (context base) method, and ensure that its parent method super.attachbasecontext (base) is called in this method to ensure that the component is loaded normally. However, it is not recommended to overload this method. If you only want to obtain the context, you can call getbasecontext () to obtain the context of the component inside the component class as long as you call attachbasecontext (contxt base).

When the application class is bound to the context environment, it indicates that the application has loaded the running environment, and the Android system will continue to call oncreate() method, indicating that the application has been created. Therefore, the custom application can overload this method to complete the operations to be performed at the initial stage of application creation. The third-party framework used in general applications will be initialized here to ensure that the framework structure is initialized when the application is created. When overloading this method, you must call its parent method super. Oncreate() first.

After that, the Android system loads and creates components according to the component information in the manifest file... This paragraph is not mentioned for the time being.

During the running of the application, when the hardware device carried by the Android system changes, the Android system will call the onconfigurationchanged (configuration newconfig) method of this class. If this method is overloaded, it can respond to the hardware device changes involved in android.content.res.configuration class, such as common horizontal and vertical screen switching, application night theme switching and normal theme switching, which can be handled here. In addition, when overloading this method, you must call its parent method super. Onconfigurationchanged (newconfig) first.

The Android system runs out of memory space, which may cause the application to fail to run normally. At this time, if the current application is still running in the foreground, the Android system will call the onlowmemory () method of this class, and then kill the background service in the application. If this method is overloaded, some unnecessary resources can be released to prevent further reduction of memory space, and the foreground interface may be retained. Similarly, when overloading this method, you must call its parent method super. Onlowmemory() first.

When the Android system runs out of memory space, the application may also run in the background. At this time, the Android system will call the ontrimmemory (int level) method of this class to indicate that the application is about to be killed by the Android system. The level parameter can indicate the level of the current application. Those with low level will be killed by the Android system first. If this method is overloaded, it can not be solved by releasing resources. Some data persistence operations can be performed to prevent data inconsistency after the application is killed and restarted. Similarly, when overloading this method, you must call its parent method super. Ontrimmemory (level) first.

It is worth noting that when the application is actively killed by the user, there is no method response in this class.

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
分享
二维码
< <上一篇
下一篇>>