Detailed introduction to the Android application class _android

Source: Internet
Author: User
Tags static class

Android Application class:

Detailed explanation for the application class in Android:

In peacetime development, we may sometimes need some global data, so that all the activity and view in the application can be accessed, when you encounter this situation, you may first think of the definition of a class, and then create many static members.
But this approach does not conform to the Android framework, but Andorid has provided us with a solution to this situation: in Android, there is a class called application, where we can use getapplication in activity () , the way to get it is to represent our application's class, use it to get the current application theme, the contents of the resource file, and so on, a more flexible feature of this class is that we can inherit to add our own global properties. Access to the entire app's activity and view.

1. Concept:

The Android system creates an object of the application class for each program runtime and creates only one, so application can be said to be a single case (singleton) A class of patterns. And the life cycle of the Application object is the longest in the entire program, and its lifecycle is equal to the lifecycle of the program. Because it is a single instance of the global, the objects obtained in different activity,service are the same object. So through application to do some, data transmission, data sharing, data caching and other operations.

2. Function:

(1). Application is a base class, and the function of the base class is to get the entire app's state, and we need to define a class to inherit the base class ourselves.
(2). Define some global and some variables and methods to be used in the context.

3. Advantages:

(1). Way of inheriting:
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 collected immediately by GC.
When you enter again you will find that the static class holds the information state before it. It may cause the program to be not the initialization state you want.
(3). When the app's process is created, the class is instantiated, and the OnCreate () method is executed, assigning an initial value to all global variables. In this way, all the activity will share the variables within the class.

The difference between 4.getContext (), Getapplication (), Getapplicationcontext (), getactivity ():

(1). GetContext (): Gets to the context of the current object.
(2). Getapplication (): Object to obtain application
(3). Getapplicationcontext (): Gets the context of the application. There is, and only one, the same object. The lifecycle is destroyed as the application is destroyed. It's like society, it's all happening in this society, and only one society. Each activity has its own context, and the entire application has only one context
(4) Getactivity (): Obtain fragment Attachment Activity object. The fragment inside the getactivity () is not recommended for use as follows: This method returns the activity attached to the current fragment, and getactivity () returns NULL when the fragment lifecycle is terminated and destroyed. So be careful when you use it to judge null or catch null pointer exceptions. So as long as the Judge Getactivity () is empty, the following code can no longer be executed, which does not affect the use of the business at all.

5. There are several scenarios in which the application creates a context instance:

(1). When the Application object is created, and the entire app is a application object
(2). When creating a service object
(3). When the activity object is created.
Activity Service application are subclasses of the context. The context is an abstract class, and the implementation is in the Contextimpl class. So the number of context numbers shared by app apps is:
Total context Instance number =service number +activity number +1 (application corresponding context instance)

6.Android memory leak problem in application:

(1). Hold an too long reference to the context. The reference to the context is more than its own life cycle. The Android application limits the amount of heap memory used to be 16M
(2). The static variable has more object references, and the memory is still not destroyed.

To sum up: to avoid the context of leakage should pay attention to the problem:

1. Use application as the context type
2. Note that the reference to the context does not exceed its own life cycle
3. Use the Static keyword with caution
4.Context if the cable, must be in the Ondestory () in time to stop.

Thank you for reading, I hope to help everyone thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.