Application and Activity,service are a system component of the Android framework, and when the Android program starts, a Application object is created to store some information about the system. Usually we do not need to specify a application, then the system will automatically help us create. Opening the manifest file for each application, you can see that the activity is included in the application tag, as follows:
1234567891011121314 |
<application
android:label=
"ViewPagerIndicator Sample"
android:icon=
"@drawable/icon"
>
<activity
android:name=
".ListSamples"
android:label=
"ViewPager Indicator"
>
<intent-filter>
<action android:name=
"android.intent.action.MAIN" />
<category android:name=
"android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
.......
......
......
|
The Android system creates an object of the application class for each program runtime and creates only one, so application is a singleton (singleton) A class of patterns. And 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 activity,service are the same object. So in Android we can avoid using static variables to store long-lasting values, and use application.
In order to make better use of this feature of application, for example we need to application to save some static values, we need to customize the class that inherits from application, and then define a variable in this class to save. The Application object is automatically generated by the application system by default, but if we customize the application, we need to tell the system that it is instantiated when we instantiate it, rather than the default. For example, we have customized a Appcontext class:
12345678 |
public class AppContext extends Application {
public static final int NETTYPE_WIFI = 0x01;
public static final int NETTYPE_CMWAP = 0x02;
public static final int NETTYPE_CMNET = 0x03;
public static final int PAGE_SIZE = 20;
//默认分页大小
private static final int CACHE_TIME = 10*60000;
//缓存失效时间
|
To get the system instantiated, we must modify the application tag attribute in manifest:
12345 |
<application android:name= ".AppContext" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/AppTheme" > |
The key is this sentence:
android:name=
".AppContext"
Usually application global objects are obtained through the context or activity's Getapplicationcontext () method, for example, we want to get the Appcontext object we just defined in our application, You need to do this in the activity:
AppContext = (appContext) this.getapplicationcontext ();
If there is a context object, you can also: AppContext = (appContext) mcontext.getapplicationcontext ();
But most of the time our code may be outside the activity and there is no reference to the context object, but we need to get the Appcontext object, and the original approach might be to try to pass the activity or context to where it needs to be called. However, the code coupling is too high and the readability is poor. We have a more elegant approach.
We talked about the Application object is global, Singleton, since is a singleton should have a class method can let us get this singleton object is, but application itself does not, we can only in the custom time to think of ways.
Application is a component of the system, it has its own life cycle function, it is surprising that his life cycle function actually has oncreate (), OnCreate is automatically called, we can use this to obtain this Application object.
Add the following lines of code to the Appcontext:
12345678910 |
private Static AppContext I nstance; public static AppContext getinstance () { return instance; } @Override public void OnCreate () { &NBSP;&NBSP;&NBSP;&NBSP; //TODO auto-generated method Stub &NBSP;&NBSP;&NBSP;&NBSP; super .oncreate (); &NBSP;&NBSP;&NBSP;&NBSP; instance = this |
This allows us to get application global objects from anywhere in the app project via Appcontext.getinstance (). For example, I define a tool class, and in the tool we need to use
The Getexternalfilesdir () method of the context. But this tool class does not have a direct way to get to the context, so we can:
1 |
return AppContext.getInstance().getExternalFilesDir( null );
|
Go Use of Android Global object application and how to get application global object anywhere