Android-global variables are very powerful, android-global variables

Source: Internet
Author: User

Android-global variables are very powerful, android-global variables

As you know, each Activity is also a Context, which is information about its execution environment in the broadest sense. your application also has a context, and Android guarantees that it will exist as a single instance using SS your application.

The way to do this is to create your own subclassAndroid. app. Application, And then specify that class in the application tag in your manifest. now Android will automatically create an instance of that class and make it available for your entire application. you can access it from any context using the Context. getApplicationContext () method (Activity also provides a method getApplication () which has the exact same effect ):

 

Java code  
  1. Class MyApp extends Application {
  2. Private String myState;
  3. Public String getState (){
  4. Return myState;
  5. }
  6. Public void setState (String s ){
  7. MyState = s;
  8. }
  9. }
  10. Class Blah extends Activity {
  11. @ Override
  12. Public void onCreate (Bundle B ){
  13. ...
  14. MyApp appState = (MyApp) getApplicationContext ());
  15. String state = appState. getState ();
  16. ...
  17. }
  18. }

 

This has essential tially the same effect as using a static variable or singleton,

But integrates quite well into the existing Android framework.

Note that this will not work until SS processes (shocould your app be one of the rare ones that has multiple processes ).

 

 

Then add the application to manifest:

Xml Code  
  1. <Application android: name = ". MyApp" android: icon = "@ drawable/icon" android: label = "@ string/app_name">
  2. <Activity android: name = ". ClickableListItemActivity"
  3. Android: label = "@ string/app_name">
  4. <Intent-filter>
  5. <Action android: name = "android. intent. action. MAIN"/>
  6. <Category android: name = "android. intent. category. LAUNCHER"/>
  7. </Intent-filter>
  8. </Activity>
  9. </Application>

Note:

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.