Application class understood in Android

Disclaimer: This article is a blogger original article, reproduced welcome but must indicate the source Thank you! https://blog.csdn.net/dongxianfei/article/details/83900380

First, Application in a Dalvik virtual machine inside there will be only one instance, so you do not need to go and get what Singleton static get Application, you put Application constructor is set to private will not be realized!

So why it is stressed that a Dalvik virtual machine, rather than an App to say it?

Because there may be multiple App a Dalvik virtual machine, which is the legendary multi-process mode. In this mode, there will be a Dalvik each Application instance, there is no relationship between them, save in the process of Application A can not get inside data in Application B process, because they do not an object, and is isolated in two a process which, so here is a stressed Dalvik virtual machine, rather than a App.

Second, the essence of an Application Context, it inherits from ContextWrapper.

android.content.Context
 	   ↳android.content.ContextWrapper
 	   	↳android.app.Application	 

When the application starts, it will first call Application.attach (), of course, this method is hidden, the first method is called developers have access to actually Application.onCreate (), we will usually this method inside the completion of various initialization, such as image loading library, Http request the default configuration library initialization and so on. But better not to be too time-consuming operation inside this method, because this will affect the startup speed App, so for unnecessary operations can use asynchronous operation, lazy loading, loading delays and other strategies to reduce the impact on the UI thread .

In addition, since () acquired by the Application object getApplicationContext in the Context, or obtained by Activity.getApplication (), Service.getApplication () to the Application, the data can be stored in the global Application, for all Activity or use the Service.

PS: the above three methods are the same acquired Application object, getApplicationContext () is the implementation class ContextImpl Context of the specific implementation, but getApplication () is in the Service Activity and implemented separately, they different scope, but get to the Application object is the same as a Dalvik virtual machine has only one Application object.

Note: it is in low memory situations, Application likely to be destroyed, resulting in data information stored in the Application which is lost, the last procedural confusion, and even Crash.
So when you want to save the data in the Application, please do the empty judgment, or select other ways to save your data.

Further, several useful methods in the present Application in such onLowMemory () and onTrimMemory (), in which these two methods, we can implement their own memory recovery logic, such as closing a database connection, and the like to remove an image memory cache operation to reduce memory consumption, thereby reducing the risk of system recovery.

Finally, it is to pay attention Application life cycle, as long as he and the Dalvik virtual machine life cycle, so during the singleton or static variable initialization operation, be sure to use Application Context is initialized as this could cause a memory leak occurs . When using Dialog Activity is generally used as a Context.

Guess you like

Origin blog.csdn.net/dongxianfei/article/details/83900380
Recommended