About the use of application under Android
1, application and Activity,service, is a system component of Android framework, when the Android program starts, the system will create a application object to store some information about the system. Usually we do not need to specify a application, then the system will automatically help us to create, if you need to create their own application, it is easy to create a class to inherit application and register in manifest application tag (just add a name attribute to the application tag to set the name of your application).
2, ndroid system will create a application class object for each program runtime and create only one, so application can be said to be 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 through the application to do some, data transfer, data sharing, such as data caching operations.
Create a DemoApplication class inheritance application implement the OnCreate () method
public class DemoApplication extends application{public String name = "Tom";p ublic void OnCreate () {super.oncreate (); LOG.V ("Zd", "CustomApplication---------------onCreate"); SetName (name);} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Call the data in the demoapplication where it is needed to achieve data sharing
public class Mainactivity extends Activity {private demoapplication app; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); LOG.V ("Zd", "Mainactivity---------------onCreate"); app = (demoapplication) getapplication (); LOG.V ("Zd", App.getname ());} protected void OnStop () {super.onstop (); LOG.V ("Zd", "Mainactivity---------------onStop");} protected void Ondestory () {Super.ondestroy (); LOG.V ("Zd", "Mainactivity---------------ondestory");}}
Adding the Name property to the application in manifest is Name= "DemoApplication"
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http// Schemas.android.com/apk/res/android " package=" com.example.test02 " android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" android:targetsdkversion= " /> <application" android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" android:name= "DemoApplication" > <activity Android:name= "Com.example.test02.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN" /> <category android: Name= "Android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
Note that you cannot perform time-consuming operations in demoapplication, or it will affect the startup time of the program
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6B/AF/wKioL1U0mDiBITExAADt2LfGYhc621.jpg "title=" Qq20150420140156.png "alt=" Wkiol1u0mdibitexaadt2lfgyhc621.jpg "/>
This article is from the "climb over the mountains to see the Sea" blog, please be sure to keep this source http://670176656.blog.51cto.com/4500575/1636012
Basic use of application