Android's application class usage

Source: Internet
Author: User

Application and Activity,service are a system component of the Android framework, and when the Android program starts, a Application object is created to store some information about the system.

The Android system automatically 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 (singleton) mode.

Usually we do not need to specify a application, the system will automatically help us to create, if you need to create their own application, it is very simple! Create a class to inherit the application and register it in the application tag in the Androidmanifest.xml file (just add the Name property to the application tag, adding your own application's name).

When application is started, a PID, the process ID, is created, and all activity is run on this process. Then we initialize the global variables when the application is created, all the activity of the same application can fetch the values of these global variables, in other words, we change the values of these global variables in one activity, Then the value of the other activity in the same application will change.

The life cycle of an Application object is the longest in the entire program, and its life cycle is equal to the program's life cycle. Because it is a global singleton, the objects obtained in different activity,service are the same object. So you can do some things through application, such as: data transfer, data sharing and data caching.

Application Scenarios:

In Android, you can implement application-level global variables by inheriting the application class, a global variable that is more secure than a static class, and will not be released until all of the app's activity is destory out.

Implementation steps:

1 , inheritance Application

 Public classCustomApplicationextendsapplication{Private Static FinalString VALUE = "Harvey"; PrivateString value; @Override Public voidonCreate () {Super. OnCreate (); SetValue (VALUE); //Initializing global Variables    }         Public voidSetValue (String value) { This. Value =value; }         PublicString GetValue () {returnvalue; }}

Note: Inheritance Application class, the main rewrite inside the onCreate () method ( android.app.Application Package of onCreate () is the real Android The entry point of the program) is the value of the initialization variable when it is created. The variable can then be manipulated in various files throughout the application.

2 , in Applicationmanifest.xml file, configure the custom Application

< Application         Android:name = "CustomApplication" > </ Application >

Instance code:

Customapplication.java

/*** Inherit application * *@authorAdmin **/ Public classCustomApplicationextendsapplication{Private Static FinalString VALUE = "Harvey"; PrivateString value; @Override Public voidonCreate () {Super. OnCreate (); SetValue (VALUE); //Initializing global Variables    }         Public voidSetValue (String value) { This. Value =value; }         PublicString GetValue () {returnvalue; }}

Firstactivity.java

 Public classFirstactivityextendsactivity{Privatecustomapplication app; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.main); App= (customapplication) getapplication ();//Get CustomApplication ObjectLOG.I ("Firstactivity", "Initial value =====" + app.getvalue ());//gets the global variable value in the process to see if it is an initialization valueApp.setvalue ("Harvey Ren");//Reset ValueLOG.I ("Firstactivity", "Modified =====" + app.getvalue ());//get the global variable value in the process again to see if it is modifiedIntent Intent=NewIntent (); Intent.setclass ( This, Secondactivity.class);    StartActivity (Intent); }}

Note: You only need to call Context of the Getapplicationcontext or Activity of the getapplication method to obtain a Application object, and then get the corresponding member variable. It is the class that represents our application, which can be used to get the contents of the currently applied theme and resource files, and so on, a more flexible feature of this class is that it can be inherited by us to add our own global properties.

Secondactivity.java

 Public classSecondactivityextendsactivity{Privatecustomapplication app; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); App= (customapplication) getapplication ();//Get ApplicationLOG.I ("Secondactivity", "Current value =====" + app.getvalue ());//Get global values    }}

Androidmanifest.xml

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.android.test"Android:versioncode= "1"Android:versionname= "1.0">    <USES-SDKandroid:minsdkversion= "8" />    <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name"Android:name= "CustomApplication">        <!--Set the default application we used to use as a custom customapplication -        <ActivityAndroid:name=". Firstactivity "Android:label= "@string/app_name">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <ActivityAndroid:name=". Secondactivity "Android:label= "@string/app_name">        </Activity>    </Application></Manifest>

Transferred from: http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html

Android's application class usage

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.