Inherit application to achieve global resource sharing

Source: Internet
Author: User

Each time the program runs, it creates an object of the application class and only one. The application disappears at the end of the app. So you can use application to do some data processing and storage. Exchange data between multiple activity or fragment such as an app's settings, themes, user information, and more. Using it to implement global variables is more convenient for centralized management of data than static.

For example:

 Public classMyAppextendsApplication {Privateperson person ; PrivateString Corpsname; PrivateList<person>Corpsmember;  Public voidSetperson (person person) { This. person=Person ; }     PublicPerson Getperson () {returnPerson ; }     Public voidSetcorps (String Corps) { This. corpsname=Corps; }     PublicString Getcorps () {returnCorpsname; }     PublicList<person>Getcorpsmember () {returnCorpsmember; }     Public voidSetcorpsmember (list<person>Corpsmember) {         This. corpsmember=Corpsmember; }}

The self-built class inherits the application class. Then modify the Android:name property in the Androidmanifest.xml file to the package name + class name

    < Application         Android:name = "COM.EXAMPLE.MYAPP.MYAPP"         android:allowbackup= "true"        android:icon= "@drawable/ic_launcher"          android:label= "@string/app_name"        android:theme= "@style/ Apptheme ">

Then you can exchange data with MyApp in various places.

        MyApp mapp= (MyApp) getapplication ();        String Corps=mapp.getcorps ();

Usually used in an activity at the beginning of the acquisition of application. For use throughout the activity. Very convenient.

     Public MyApp Mapp;    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {        super  . OnCreate (savedinstancestate);        Setcontentview (R.layout.person);        Mapp=(MyApp) getapplication ();            }

But be careful not to pass in the object that holds the reference to the activity. This will not be recycled by GC after this activity has ended, causing a memory leak.

The use of the application class is extensive. The following example shows the convenience of activity management when there is more activity.

 Public classMreapplicationextendsApplication { Public Static FinalString tag= "Mre"; /**store information that holds the state of each activity, used to automatically revert to the last access state when activity is new or returned, key for each activity class name **/    PrivateHashmap<string, Hashmap<string, object>>activitystate; /**record the activity's stack, save the Activity class name **/    PrivateStack<string>Activitystack;  PublicHashmap<string, Hashmap<string, object>>getactivitystate () {returnactivitystate; }     Public voidSetactivitystate (hashmap<string, hashmap<string, object>>activitystate) {         This. activitystate =activitystate; }     PublicStack<string>Getactivitystack () {returnActivitystack; }     Public voidSetactivitystack (stack<string>activitystack) {         This. Activitystack =Activitystack; }    /*** Delete the last activity in the stack*/     Public voidpopactivity () {String activity=activitystack.lastelement (); if(Activity! =NULL) {activitystack.remove (activity); Activity=NULL; }    }    /*** Delete specified activity in stack*/     Public voidpopactivity (String activity) {if(Activity! =NULL) {activitystack.remove (activity); Activity=NULL; }    }    /*** Take Current Activity*/     PublicString currentactivity () {if(Activitystack = =NULL) {            return NULL; } String Activity=activitystack.lastelement (); returnactivity; }    /*** Join Activity*/     Public voidpushactivity (String activity) {if(Activitystack = =NULL) {Activitystack=NewStack<string>();    } activitystack.add (activity); }}

 

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.