Global Variables for Android

Source: Internet
Author: User

Because to use the log login name, and then use between the various interfaces, at the beginning, we use the intent interface between the value of the transfer, but the effect is not very good, on the internet to see a such a post is very good, and intends to record over. (Reprinted from http://blog.csdn.net/yuzhiboyi/article/details/7692698)

For example, if you want to use global variables throughout your application, you typically use static variables, public types in Java, and if you use such global variables in Android, you don't conform to the Android framework. But a more elegant way of using application context is to use it.

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 would cause that class to being instantiated for your when 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 anifest.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:

1  PackageCom.jikexuyuan.usingslidingmenu;2 3 4 Importandroid.app.Application; 5 6  Public classYappextendsapplication{7   8     PrivateString Ylabel; 9      PublicString Getlabel () {Ten         returnYlabel;  One     }      A      Public voidSetLabel (String s) { -          This. Ylabel =s;  -     }   the    - @Override -      Public voidonCreate () { -         //TODO auto-generated Method Stub +         Super. OnCreate ();  -SetLabel ("Sno");//Initializing global Variables +     }      A}

To remove or re-assign a value:

1 PrivateYapp Yapp;2     /**Called when the activity is first created.*/3 @Override4      Public voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 requestwindowfeature (Window.feature_no_title); 7 Setcontentview (r.layout.activity_login);8        9         TenYAPP = (yApp) getapplication ();//Get your custom application Yapp OneLOG.I ("Yangl", "Initlabel:" +yapp.getlabel ());//take out the global variables we put into the process to see if we've ever set a value

Then modify the configuration file Applicationmanifest.xml and add the application Yapp to run:

1  <!-- here, set the default application to your own Yapp  -  2     <  android:name= "YApp"  3        android:icon= "@ Drawable/icon "  4        android:label=" @string/app_name "  5          >  

In this case, you can use this value on every interface.

Global Variables for Android

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.