Application Usage in Android

Source: Internet
Author: User

ApplicationClass

Application is a system component of the android framework like activity and service. When the android program starts, the system creates an application object to store some information about the system.

The Android system automatically creates an application Class Object for each program running, so the application can be called a class in singleton mode.

Generally, we do not need to specify an application. The system will automatically create it for us. If you need to create your own application, it is also very easy! Create a class that inherits the application and registers it in the application tag in the androidmanifest. xml file (you only need to add the name attribute to the application tag and add your own application name ).

When the application is started, the system creates a PID, that is, the process ID, and all the activities will run on this process. Then we initialize the global variables when the application is created. All the activities of the same application can obtain the values of these global variables. In other words, if we change the values of these global variables in an activity, the values of other activities in the same application will change.

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 activities and services are all the same object. Therefore, you can perform some operations through the application, such as data transmission, data sharing, and data cache.

Application scenarios:

In Android, You can inherit the application class to implement global variables at the application level. This global variable method is more secure than the static class, it will not be released until all the activities of the application are destory.

Steps:

1, InheritanceApplication

Public class customapplication extends application {Private Static final string value = "Harvey"; private string value; @ override public void oncreate () {super. oncreate (); setvalue (value); // initialize the global variable} public void setvalue (string value) {This. value = value ;}public string getvalue () {return value ;}}

Note: InheritanceApplicationClass, mainly rewritingOncreate() Method (Android. App. ApplicationPackageOncreate() Is trueAndroidThe entry point of the program. Then the variable can be operated on in each file of the entire application.

2InApplicationmanifest. xmlConfigure the customApplication

<application        android:name="CustomApplication"></application>

Instance code:

Customapplication. Java

/*** Inherit application ** @ author admin **/public class customapplication extends application {Private Static final string value = "Harvey"; private string value; @ override public void oncreate () {super. oncreate (); setvalue (value); // initialize the global variable} public void setvalue (string value) {This. value = value ;}public string getvalue () {return value ;}}

Firstactivity. Java

Public class firstactivity extends activity {private customapplication app; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); APP = (customapplication) getapplication (); // obtain the customapplication object log. I ("firstactivity", "initial value =" + app. getvalue (); // obtain the global variable value in the process to check whether the value is app. setvalue ("Harvey Ren"); // reset the value log. I ("firstactivity", "modified ====" + app. getvalue (); // obtain the global variable value in the process again to check whether intent = new intent (); intent. setclass (this, secondactivity. class); startactivity (intent );}}

Note: you only need to callContextOfGetapplicationcontextOrActivityOfGetapplicationMethod to obtainApplicationObject, and then get the corresponding member variable.It is a class that represents our application. It can be used to obtain the topic of the current application and the content in the resource file. A more flexible feature of this class is that it can be inherited by us, to add our own Global attributes.

Secondactivity. Java

Public class secondactivity extends activity {private customapplication app; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. Main );
APP = (customapplication) getapplication (); // get the application

Log. I ("secondactivity", "current value =" + app. getvalue (); // obtain global value }}

Androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. android. test "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name" Android: Name = "customapplication"> <! -- Set the previously used Default Application to custom customapplication --> <activity Android: Name = ". firstactivity "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> <activity Android: Name = ". secondactivity "Android: Label =" @ string/app_name "> </activity> </Application> </manifest>
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.