Application class
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.
The Android system automatically creates an object of the application class for each program runtime and creates only one, so application can be said to be a class of Singleton (singleton) mode.
Usually we do not need to specify a application, the system will automatically help us to create, if you need to create their own application, it is very simple! Create a class to inherit the application and register it in the application tag in the Androidmanifest.xml file (just add the Name property to the application tag, adding your own application's name).
When application is started, a PID, the process ID, is created, and all activity is run on this process. Then we initialize the global variables when the application is created, all the activity of the same application can fetch the values of these global variables, in other words, we change the values of these global variables in one activity, Then the value of the other activity in the same application will change.
The life cycle of an Application object is the longest in the entire program, and its life cycle is equal to the program's life cycle. Because it is a global singleton, the objects obtained in different activity,service are the same object. So you can do some things through application, such as: data transfer, data sharing and data caching.
Application Scenarios:
In Android, you can implement application-level global variables by inheriting the application class, a global variable that is more secure than a static class, and will not be released until all of the app's activity is destory out.
Implementation steps:
1 , inheritance Application
public class CustomApplication extends application{ private static final String VALUE = "Harvey"; private String value; @Override public void OnCreate () { super.oncreate (); SetValue (VALUE); Initialize global variable } public void SetValue (String value) { this.value = value; } Public String GetValue () { return value; }}
Note: Inheritance Application class, the main rewrite inside the onCreate () method ( android.app.Application Package of onCreate () is the real Android The entry point of the program) is the value of the initialization variable when it is created. The variable can then be manipulated in various files throughout the application.
2 , in Applicationmanifest.xml file, configure the custom Application
<application android:name= "CustomApplication" ></application>
Instance code:
Customapplication.java
/** * Inherit application * * @author Admin * */public class CustomApplication extends application{ private static F inal String VALUE = "Harvey"; private String value; @Override public void OnCreate () { super.oncreate (); SetValue (VALUE); Initialize global variable } public void SetValue (String value) { this.value = value; } Public String GetValue () { return value; }}
Firstactivity.java
public class Firstactivity extends activity{private customapplication app; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); App = (customapplication) getapplication (); Get CustomApplication Object log.i ("Firstactivity", "Initial value =====" + app.getvalue ()); Gets the global variable value in the process to see if it is an initialization value app.setvalue ("Harvey Ren"); Reset Value log.i ("Firstactivity", "Modified =====" + app.getvalue ()); Get the global variable value in the process again to see if it is modified Intent Intent = new Intent (); Intent.setclass (this, secondactivity.class); StartActivity (Intent); }}
Note: You only need to call Context of the Getapplicationcontext or Activity of the getapplication method to obtain a Application object, and then get the corresponding member variable. It is the class that represents our application, which can be used to get the contents of the currently applied theme and resource files, and so on, a more flexible feature of this class is that it can be inherited by us to add our own global properties.
Secondactivity.java
public class Secondactivity extends activity{ private customapplication app; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.main);
App = (customapplication) getapplication (); Get application
LOG.I ("secondactivity", "Current value =====" + app.getvalue ()); Get global Value }}
Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.android.test "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8"/> <application android:icon= "@drawable/icon" android:label= "@string/app_ Name "Android:name=" CustomApplication > <!--Set the default application we used to use as a custom customapplication- <activity android:name= ". Firstactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android: Name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activi Ty Android:name= ". Secondactivity "android:label=" @string/app_name "> </ACTIVITY> </application></manifest>
Application class usage in Android