Android Application class shares global data

Source: Internet
Author: User

The Android system creates an object of the application class for each program runtime and creates only one, so application can be said to be a class of singleton patterns. And the life cycle of the Application object is the longest in the whole program, and its life cycle is equal to the life cycle of the program. Because it is a global singleton, the objects obtained in different activity,service are the same object. So through the application to do some, data transfer, data sharing, such as data caching operations, the code is as follows:

Package Com.example.five;import;public class MYAPP extends application{private int type;public int GetType () {return type;} public void Settype (int x) {this.type=x;} @Overridepublic void OnCreate () {super.oncreate (); settype (0);}}
Then specify the application class in the Androidmanifest.xml file:

<application   android:name= ". MyApp "     android:icon=" @drawable/icon "     
Value stored in activity:

(MYAPP) getapplication ()). Settype (1);
Value in activity:

(MYAPP) getapplication ()). GetType ();
Value in view:

(MYAPP) GetContext (). Getapplicationcontext ()). GetType ();

Android Application class shares global data

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: 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.