Android Application summary, androidapplication

Source: Internet
Author: User

Android Application summary, androidapplication
Application configuration global Context

Step 1: Write a global Singleton mode. MyApplication inherits from Application overwrite onCreate, and instantiate Application in this method.

Step 2: configure the global Context

<Application android: name = "com. appstore. service. MyApplication"> </application>

Step 3: Use. Access Context by class name when using it

Entry point of the Android Program

Android uses Google Dalvik VM, which is very different from traditional Java VM. In Sun's Java System, the entry point is the same as that in Standard C language (), each Android program contains an Application instance, which contains multiple activities, services, ContentProvider, and Broadcast referers. Because most applications contain Activity, many netizens think it is onCreate of Activity. But have you found that there are multiple activities in your project? You may not have seen any Android app without Activity.

In fact, onCreate In the android. app. Application package is the real Android entry point, but most developers do not need to rewrite this class. Its inheritance relationship is as follows:

Java. lang. Object
? Android. content. Context
? Android. content. ContextWrapper
? Android. app. Application

The android. app. Application class contains four public methods.

Void onConfigurationChanged (Configuration newConfig)
Void onCreate () // here is the real entry point.
Void onLowMemory ()
Void onTerminate ()

So I hope you will remember that the real Android portal is application main. You can see the inclusion relationship of androidmanifest. xml clearly. Not every application must have Activity.

Global variables of application in android

In android programming, the term "application" seems to be so uncommon, but more familiar to everyone is activity, intent, provider, broadcast, and service. But in fact, the application in android also has its own use.

Open the manifest file and you will see an application configuration tag, which is used for application. So what is the purpose of application? To see how the SDK describes:

Base class for those who need to maintain global application state. you can provide your own implementation by specifying its name in your AndroidManifest. xml's <application> tag, which will cause that class to be instantiated for you when the process for your application/package is created.

That is to say, the application is used to save global variables and will exist when the package is created. Therefore, when we need to create global variables, we do not need to create static variables with public permissions as j2se, but directly implement them in the application. You only need to call the getApplicationContext of Context or the getApplication method of Activity to obtain an application object and then process it accordingly.

Because many files are involved in small projects, I Will paste the code here.

Application file:

Java code:

Public class TestApplication extends Application {

Private int curIndex;

Public int getCurIndex (){

Return curIndex;

}

Public void setCurIndex (int curIndex ){

This. curIndex = curIndex;

}

@ Override

Public void onCreate (){

Super. onCreate ();

}

@ Override

Public void onTerminate (){

Super. onTerminate ();

}

}

The application has a curIndex and setter getter method.

The first acitivty operation on the application:

Java code:

TestApplication application = (TestApplication) this. getApplication ();

Log. I ("data", "" + application. getCurIndex ());

Application. setCurIndex (5 );

The second Activity:

Java code:

TestApplication application = (TestApplication) this. getApplication ();

Log. I ("data", "" + application. getCurIndex ());

Application. setCurIndex (6 );

Third Activity:

Java code

Final TestApplication application = (TestApplication) this. getApplication ();

Log. I ("data", "" + application. getCurIndex ());

In the running process, each time the corresponding Activity is killed, and then enters the next 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.