Android Application Class Brief introduction (i)

Source: Internet
Author: User
Tags i18n

Every time the application executes. The application class of the application keeps the state of the instantiation.

By extending the Applicaiton class, you can complete the following 3 tasks:

1. Respond to application-level events such as low and low on Android execution time broadcasts.

2. Passing objects (global variables) between application components.

3. Manage and maintain resources used by multiple application components.

The latter two tasks are better done by using a singleton class. Application is instantiated when the application process is created.

The following is a demo sample code for extending application:

Import Android.app.application;public class MyApplication extends application {private static MyApplication singleton;/ /return application instance public static MyApplication getinstance () {return singleton;} @Overridepublic void OnCreate () {super.oncreate (); singleton = this;}}
After creating your own application, in the mainfest inside the application register. For example, the following:

<application        android:allowbackup= "true"        android:icon= "@drawable/ic_launcher"        android:label= "@ String/app_name "        android:name=" com.example.i18n.MyApplication "        android:theme=" @style/apptheme ">

As for Get and set:

If MyApplication has variable str, and provides getter and setter, for example the following:

Package Com.example.i18n;import Android.app.application;public Class MyApplication extends application {private static MyApplication singleton;private String str;//Returns the application instance public static MyApplication getinstance () {return singleton;} @Overridepublic void OnCreate () {super.oncreate (); singleton = this;} Public String Getstr () {return str;} public void Setstr (String str) {this.str = str;}}

Use STR and assign values:

Myapplication.getinstance (). SETSTR ("hello,bitch!"); String mystr = Myapplication.getinstance (). GETSTR (); LOG.E ("str", mystr+ "");


Write it down here first.

Good night.

Android Application Class Brief introduction (i)

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.