Each time the application runs, the application class of the application remains instantiated. By extending the Applicaiton class, you can accomplish the following 3 tasks:
1. Respond to application-level events such as low and low on Android runtime 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 an example 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, as follows:
<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, as follows:
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.