Recently in the project, encountered application This class, began not to know what to use, after learning to know that its use is quite large, for example, if you want to use global variables throughout the application, in Java is generally used static variables, public type In Android, if you use such a global variable does not conform to the Android framework architecture, but you can use a more elegant way is to use the application context.
Let's take a look at this note first:
Base class for those who need to maintain global application state. You
can provide your own implementation by specifying it name in your
Androidmanifest.xml ' s <application> tag, which'll cause that class
To being instantiated for if the process for your application/package is
Created.
This means: The application class is a base class that is intended to get the state of the entire application. You can inherit or implement this class yourself, when you want to use your own extended application class, as long as the manifest.xml in the <application> tag in the name of the class to apply their own definition of the line, The result of this is that the class is instantiated when your application or the process in which the package is created.
How do you use it? First you need to rewrite the application, the main rewrite inside the OnCreate method, that is, when creating the value of the initialization variable. The variable can then be manipulated in various files throughout the application. 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. Let's take a look at the application steps in detail:
The code is as follows:
[Java] view Plaincopy
- Yy.android.yapp;
- Import android.app.Application;
- Public class YApp extends application{
- private String Ylabel;
- Public String Getlabel () {
- return ylabel;
- }
- public void SetLabel (String s) {
- This.ylabel = s;
- }
- @Override
- public void OnCreate () {
- //TODO auto-generated method stub
- super.oncreate ();
- SetLabel ("yuzhiboyi_csnd!"); //Initialize global variables
- }
- }
Here's Mainactivity.java.
[Java] view Plaincopy
- Package Yy.android.yapp;
- Import android.app.Activity;
- Import android.content.Intent;
- Import Android.os.Bundle;
- Import Android.util.Log;
- Public class Mainactivity extends Activity {
- private YApp YApp;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- YAPP = (yApp) getapplication (); //Get custom application Yapp
- LOG.I ("Yangl", "Initlabel:" +yapp.getlabel ()); //Put us in the process of the global variables to take out, to see whether we have set the value
- Yapp.setlabel ("yuzhiboyi!"); //Modify
- LOG.I ("YAnG", "Changelabel:" +yapp.getlabel ()); //Look, this value has changed no
- Intent Intent = new Intent (); //Take a look at the initialized value in another activity, or take the modified value
- Intent.setclass (This, otheractivity. Class);
- StartActivity (Intent);
- }
- }
Another otheractivity.java:
[Java] view Plaincopy
- Package Yy.android.yapp;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.util.Log;
- Public class Otheractivity extends activity{
- private YApp YApp;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- YAPP = (yApp) getapplication (); //Get the custom application MyApp
- LOG.I ("YAnG", "Ohteractivity receive the Label:" +yapp.getlabel ()); //See if the variable value has been modified
- }
- }
To modify the configuration file Applicationmanifest.xml, add the application Yapp that will be run:
[HTML] view plaincopy
- <? 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">
- <!--here, set the default application to your own yapp-->
- <application android:name="YApp"
- android:icon="@drawable/icon"
- android:label="@string/app_name"
- >
- <activity android:name=". 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>
- <activity android:name=". Otheractivity">
- </Activity>
- </Application>
- </manifest>
Running results: 03-04 16:53:17.295:info/guoll (650): initlabel:yuzhiboyi_csnd! 03-04 16:53:17.295:info/guoll (650): Changelabel:yuzhiboyi 03-04 16:53:17.426:info/guoll (650): OhterActivity receive The Label:yuzhiboyi
All right, use it!