Using the application class to implement global variables in Android

Source: Internet
Author: User

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
  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 global variables
  16. }
  17. }

Here's 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 YApp;
  8. @Override
  9. public void OnCreate (Bundle savedinstancestate) {
  10. super.oncreate (savedinstancestate);
  11. Setcontentview (R.layout.main);
  12. YAPP = (yApp) getapplication (); //Get custom application Yapp
  13. 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
  14. Yapp.setlabel ("yuzhiboyi!"); //Modify
  15. LOG.I ("YAnG", "Changelabel:" +yapp.getlabel ()); //Look, this value has changed no
  16. Intent Intent = new Intent (); //Take a look at the initialized value in another activity, or take the modified value
  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 YApp;
  7. @Override
  8. protected void OnCreate (Bundle savedinstancestate) {
  9. super.oncreate (savedinstancestate);
  10. Setcontentview (R.layout.main);
  11. YAPP = (yApp) getapplication (); //Get the custom application MyApp
  12. LOG.I ("YAnG", "Ohteractivity receive the Label:" +yapp.getlabel ()); //See if the variable value has been modified
  13. }
  14. }

To modify the configuration file Applicationmanifest.xml, add the application Yapp that will 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">
  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 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!

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.