Android uses the application class to save the app's global data

Source: Internet
Author: User

In practical applications we often need to interact with and save the data, but the default method in intent has a type limit on the transmission of data, when we need to transfer or save a complex generic data, the use of application is a good solution.

As the name implies, application corresponds to the entire application, just as the activity corresponds to the current active window, and when we need to use it, be sure to declare it in Androidmanifest.xml.

 <application android:name  = " meterapplication    Android: Allowbackup  = true   "  Android:icon  ="   @drawable/ic_launcher   "  Android:label  =  @string/app_name   "  Android:theme  ="   @style/apptheme   > </application> 

Then we come to the entity class, first, when other classes need to be accessed or called, first call this class of getinstance () to get Meterapplication objects, the next can be different types of data get,set operation, so the benefits of the form is not Limitations of general data types, when we need to save or read complex data across one or more activity, the problem is solved.

Calling method: Meterapplication.getinstance (). GetXXX (); Read

Meterapplication.getinstance (). setxxx (X XXX);//Deposit

 Public classMeterapplication extends Application {PrivateList<readytask>downloadlist; Private StaticMeterapplication instance; PrivateHashmap<string, list<readytask>>Downloadresult; PrivateList<string>unitnumlist; PrivateString Curopnum; PrivateBoolean blueconstatus; Private intcurstauts;//Current Running State@Override Public voidonCreate () {//TODO auto-generated Method StubInstance = This; Downloadlist=NewArraylist<readytask>();    Super.oncreate (); }     PublicBoolean isblueconstatus () {returnBlueconstatus; }     Public voidSetblueconstatus (Boolean blueconstatus) {blueconstatus=Blueconstatus; }     PublicList<string>getunitnumlist () {returnunitnumlist; }     Public voidSetunitnumlist (list<string>unitnumlist) {Unitnumlist=unitnumlist; }     PublicHashmap<string, list<readytask>>Getdownloadresult () {returnDownloadresult; }     Public voidSetdownloadresult (hashmap<string, list<readytask>>Downloadresult) {Downloadresult=Downloadresult; }     Public Staticmeterapplication getinstance () {returninstance; }     PublicList<readytask> Getdownloadlist () {//Download the value of list        returndownloadlist; }     Public voidSetdownloadlist (list<readytask>downloadlist) {downloadlist=downloadlist; }     Public intgetcurstauts () {returncurstauts; }     Public voidSetcurstauts (intcurstauts) {curstauts=curstauts; }     PublicString Getcuropnum () {returnCuropnum; }     Public voidsetcuropnum (String curopnum) {curopnum=Curopnum; }         }

Android uses the application class to save the app's 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: 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.