Android Application class: androidapplication
Detailed explanation of the Application class in Android:
During normal development, we may sometimes need some global data to make all the activities and views in the application accessible. In this case, you may first want to define a class and then create manyStatic Member.
However, this method does not conform to the Android framework architecture, but andorid already provides us with a solution for this situation: in Android, there isApplicationClass, which can be used in the ActivityGetApplication ()Is a class that represents our application. You can use it to obtain the topic of the current application, content in the resource file, and so on, A more flexible feature of this class is that it can be inherited by us to add our own Global attributes. Make the Activity and View of the entire App accessible.
1. concept:
The android system creates only one Application Class Object for each program running. Therefore, the Application is a class in singleton mode. the life cycle of the application object is the longest in the whole program, and its life cycle is equal to the life cycle of the program.Because it is a global Singleton, the objects obtained in different activities and services are all the same object.Therefore, you can perform some operations through the Application,Data transmission, data sharing, and data caching.
2. role:
(1). Application is a base class. The function of this base class is to obtain the status of the entire App. We need to define a class to inherit this base class.
(2) define some global variables and methods used in the context.
3. Advantages:
(1). Inheritance Method:
The lifecycle is destroyed as the application is destroyed.
(2). Static class or static method:
After the program exits, the class or variable cannot be recycled by GC immediately.
When you enter the static class again, you will find that the information saved by the static class is in the previous state. The program may not be initialized as you want.
(3)When the. App process is created,This class will be instantiated, And the onCreate () method will be executedTo assign initial values to all global variables. In this way,All activities share the variables in this class.
4. Differences between getContext (), getApplication (), getApplicationContext (), and getActivity:
(1). getContext ():Obtains the context of the current object.
(2). getApplication ():Get the Application Object
(3). getApplicationContext ():Obtain the context of the application. There is only one and only one identical object. The lifecycle is destroyed as the application is destroyed. It is like a society where everything happens and there is only one society.Each Activity has its own context, and the entire application only has one context.
(4) getActivity ():Obtains the Activity object attached to Fragment. GetActivity () in Fragment is not recommended for the following reasons: This method returns the Activity appended to the current Fragment. When the Fragment lifecycle ends and is destroyed, getActivity () returns null, therefore, when using it, you must be sure to judge null or capture null pointer exceptions. Therefore, if getActivity () is null, the following code is no longer executed, which does not affect the service usage.
5. The application creates a Context instance in the following situations:
(1 ).When creating an Application object, and the entire App has a total of Application objects
(2 ).When creating a Service object
(3 ).When creating an Activity object.
The Activity Service Application is a subclass of Context. Context is an abstract class. The specific implementation is in the ContextImpl class. Therefore, the formula for the total number of Context values in an App is as follows:
Total number of Context instances = Service count + Activity count + 1 (Context instance corresponding to Application)
6. Memory leakage in Android applications:
(1 ).Hold a long reference to Context. The reference to Context exceeds its own lifecycle. The heap memory limit for Android applications is 16 Mb.
(2 ).Static variables have more object references, and the memory will not be destroyed.
To sum up, pay attention to the following issues to avoid Context leakage:
1.Use the Context type of Application
2.Note that the reference to Context should not exceed its own lifecycle.
3.Exercise caution when using static keywords
4If there is a thread in. Context, it must be stopped in onDestory () in time.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.