android--Global variables very good, very powerful.

Source: Internet
Author: User

As you are know, each Activity is also a Context, which was information about its execution environment in the broadest sense. Your application also have a context, and Android guarantees that it'll exist as a single instance across Your Applicatio N.

The the-the-do-is-to- create your own subclass of android.app.Application , and then specify this class in the application tag in your manifest. Now Android would automatically create an instance of this 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 Getap Plication () 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 have essentially the same effect as using a static variable or singleton,

But integrates quite well into the existing Android framework.

Note that this won't work across processes (should your app is one of the rare ones that have multiple processes).

Then add the app in 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>

Description

    1. What you need to add: android:name= ". Your_app_name"

    2. Location: Where the current activity is, (I just started thinking I need to create a new <application></application>)

android--Global variables very good, very powerful.

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.