Every time the application executes. The application class of the application keeps the state of the instantiation.
By extending the Applicaiton class, you can complete the following 3 tasks:
1. Respond to application-level events such as low and low on Android execution time broadcasts.
2. Passing objects (global variables) between application components.
3. Manage and maintain resources used by multiple application components.
The latter two tasks are better done by using a singleton class. Application is instantiated when the application process is created.
The following is a demo sample code for extending application:
Import Android.app.application;public class MyApplication extends application {private static MyApplication singleton;/ /return application instance public static MyApplication getinstance () {return singleton;} @Overridepublic void OnCreate () {super.oncreate (); singleton = this;}}
After creating your own application, in the mainfest inside the application register. For example, the following:
<application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@ String/app_name " android:name=" com.example.i18n.MyApplication " android:theme=" @style/apptheme ">
As for Get and set:
If MyApplication has variable str, and provides getter and setter, for example the following:
Package Com.example.i18n;import Android.app.application;public Class MyApplication extends application {private static MyApplication singleton;private String str;//Returns the application instance public static MyApplication getinstance () {return singleton;} @Overridepublic void OnCreate () {super.oncreate (); singleton = this;} Public String Getstr () {return str;} public void Setstr (String str) {this.str = str;}}
Use STR and assign values:
Myapplication.getinstance (). SETSTR ("hello,bitch!"); String mystr = Myapplication.getinstance (). GETSTR (); LOG.E ("str", mystr+ "");
Write it down here first.
Good night.
Android Application Class Brief introduction (i)