Recently, I encountered the application class in the project. I didn't know how to use it at first. I learned how to use it. For example, if you want to use global variables in the entire application, static variables and public variables are generally used in Java. If you use such global variables in Android, they do not conform to the framework architecture of Android, however, you can use application context in a more elegant way.
Let's take a look at this description:
Base class for those who need to maintain global application state. You
Can provide your own implementation by specifying its name in your
Androidmanifest. XML's & lt; Application & gt; tag, which will cause that class
To be instantiated for you when the process for your application/package is
Created.
The application class is a base class, which is used to obtain the state of the entire application. You can inherit or implement this class by yourself. When you want to use your own extended application class, you only need. in XML, the <Application> label in the name application defines the class. The result is: when your application or the process of the package is created, this class will be instantiated.
How to use it? First, you must override the application. The oncreate method is used to override the value of the variable during creation. Then the variable can be operated on in each file of the entire application.
When the application is started, the system creates a PID, that is, the process ID, and all the activities will run on this process. Then we initialize the global variables when the application is created. All the activities of the same application can obtain the values of these global variables. In other words, if we change the values of these global variables in an activity, the values of other activities in the same application will change. The following is an example of detailed application steps:
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 the global variable
- }
- }
Below is 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;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Yapp = (yapp) getapplication (); // obtain the Custom Application yapp
- Log. I ("yangl", "initlabel:" + yapp. getlabel (); // take out the global variables we put in the process to see if they were the values we set.
- Yapp. setlabel ("yuzhiboyi! "); // Modify it
- Log. I ("yang", "changelabel:" + yapp. getlabel (); // check whether the value has changed
- Intent intent = new intent (); // check whether the initialization value is obtained in another activity or the modified value is obtained.
- 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;
- @ Override
- Protected void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Yapp = (yapp) getapplication (); // obtain the Custom Application MyApp
- Log. I ("yang", "ohteractivity receive the label:" + yapp. getlabel (); // check whether the variable value has been modified
- }
- }
Modify the configuration file applicationmanifest. xml and add the application yapp to 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" type = "codeph" text = "/codeph">
- <! -- 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 result:
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
Okay, that's how it works!