Use of Android global variables

Source: Internet
Author: User

Usage scenarios:

Need to share data between multiple activity or service

1. Custom Application

 Public class extends Application {     privatenew hashmap<string, object>();                Public void put (String Key,object object) {         map.put (key, object);     }                Public Object get (String key) {         return  map.get (key);     }}

2. Modify the Androidmanifest.xml file:

<application <strong><em>android:name= "DemoApplication" </em></strong> android:icon= "@ Drawable/app "android:label=" @string/app_name ">

Note the contents of the black bold character, specifying that the program uses our own defined application.

3. Use:

Save data to a map defined in application in one of the activity

DemoApplication app = (demoapplication) getapplication () app.put ("key", "This is a test!");

Then get the data in another activity:

DemoApplication app == (String) app.get ("key"); Toast.maketext (This, value, Toast.length_long). Show ();

Note that concurrency issues are not considered here and, if needed, can be implemented by adding the associated synchronization code to the custom application.

Use of Android global variables

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.