Use the application class in Android to implement the use of global data variables

Source: Internet
Author: User

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
  1. YY. Android. yapp;
  2. Import Android. App. Application;
  3. Public class yapp extends application {
  4. Private string ylabel;
  5. Public String getlabel (){
  6. Return ylabel;
  7. }
  8. Public void setlabel (string s ){
  9. This. ylabel = s;
  10. }
  11. @ Override
  12. Public void oncreate (){
  13. // Todo auto-generated method stub
  14. Super. oncreate ();
  15. Setlabel ("yuzhiboyi_csnd! "); // Initialize the global variable
  16. }
  17. }


Below is mainactivity. Java

[Java]
View plaincopy
  1. Package yy. Android. yapp;
  2. Import Android. App. activity;
  3. Import Android. content. intent;
  4. Import Android. OS. Bundle;
  5. Import Android. util. log;
  6. Public class mainactivity extends activity {
  7. Private yapp;
  8. @ Override
  9. Public void oncreate (bundle savedinstancestate ){
  10. Super. oncreate (savedinstancestate );
  11. Setcontentview (R. layout. Main );
  12. Yapp = (yapp) getapplication (); // obtain the Custom Application yapp
  13. 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.
  14. Yapp. setlabel ("yuzhiboyi! "); // Modify it
  15. Log. I ("yang", "changelabel:" + yapp. getlabel (); // check whether the value has changed
  16. Intent intent = new intent (); // check whether the initialization value is obtained in another activity or the modified value is obtained.
  17. Intent. setclass (this, otheractivity. Class );
  18. Startactivity (intent );
  19. }
  20. }


Another otheractivity. Java:

[Java]
View plaincopy
  1. Package yy. Android. yapp;
  2. Import Android. App. activity;
  3. Import Android. OS. Bundle;
  4. Import Android. util. log;
  5. Public class otheractivity extends activity {
  6. Private yapp;
  7. @ Override
  8. Protected void oncreate (bundle savedinstancestate ){
  9. Super. oncreate (savedinstancestate );
  10. Setcontentview (R. layout. Main );
  11. Yapp = (yapp) getapplication (); // obtain the Custom Application MyApp
  12. Log. I ("yang", "ohteractivity receive the label:" + yapp. getlabel (); // check whether the variable value has been modified
  13. }
  14. }

Modify the configuration file applicationmanifest. xml and add the application yapp to be run:

[HTML]
View plaincopy
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Package = "com. Android. test"
  4. Android: versioncode = "1"
  5. Android: versionname = "1.0" type = "codeph" text = "/codeph">
  6. <! -- Here, set the default application to your own yapp -->
  7. <Application Android: Name = "yapp"
  8. Android: icon = "@ drawable/icon"
  9. Android: Label = "@ string/app_name"
  10. >
  11. <Activity Android: Name = ". mainactivity"
  12. Android: Label = "@ string/app_name">
  13. <Intent-filter>
  14. <Action Android: Name = "android. Intent. Action. Main"/>
  15. <Category Android: Name = "android. Intent. Category. launcher"/>
  16. </Intent-filter>
  17. </Activity>
  18. <Activity Android: Name = ". otheractivity">
  19. </Activity>
  20. </Application>
  21. </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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.