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 class of singleton 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 through the application to do some, data transfer, data sharing, such as data caching operations, the code is as follows:
Package Com.example.five;import Android.app.application;public class MYAPP extends application{private int type;public int GetType () {return type;} public void Settype (int x) {this.type=x;} @Overridepublic void OnCreate () {super.oncreate (); settype (0);}}
Then specify the application class in the Androidmanifest.xml file:
<application android:name= ". MyApp " android:icon=" @drawable/icon "
Value stored in activity:
(MYAPP) getapplication ()). Settype (1);
Value in activity:
(MYAPP) getapplication ()). GetType ();
Value in view:
(MYAPP) GetContext (). Getapplicationcontext ()). GetType ();
Android Application class shares global data