Android Use Application Summary

Source: Internet
Author: User

application Configuring the global context

The first step, write a global singleton pattern of the MyApplication inherits from the application overlay OnCreate, in this method is instantiated application

Second step, configure the global context

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

The third step, use, when used when using the name of the class to access the context

entry points for Android programs

Android uses a Google Dalvik VM, which differs greatly from traditional Java VMs in that the entry point in Sun's Java system is main () as in the standard C language, and each Android program contains a application instance. There are multiple Activity, Service, ContentProvider, or broadcast Receiver in a application instance. Because most of the applications contain activity so, said many netizens think is the activity of OnCreate, but you did not find that your project has a number of activity? You may not have seen an Android app without activity.

In fact, in android.app.Application this package oncreate is the real Android entry point, but most developers do not have to rewrite the class, his inheritance relationship such as:

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

The Android.app.Application class contains 4 public methods

void onconfigurationchanged (Configuration newconfig)
void OnCreate ()//This is the real entry point.
void Onlowmemory ()
void Onterminate ()

So I hope everyone, remember that the real Android entry point is application main, you can see the androidmanifest.xml of the inclusion of the relationship is clear, not every application must have activity.

application about global variables in Android

In Android programming, nouns such as application seem to become less common, and the activity, intent, provider, broadcast, and service are more familiar to everyone. But in fact, Android application also has its own use.

Open the manifest file and you will see a application configuration tag, which is about the use of application. What is the use of application? Take a look at how it is described in the SDK:

Base class for those who need to maintain global application state. You can provide your own implementation by specifying it name in your Androidmanifest.xml ' s < application> tag, whi CH would cause that class to being instantiated for your when the process for your application/package is created.

This means that application is used to hold global variables, and then it exists when the package is created. So when we need to create global variables, we do not need to create the static variables of public permissions as J2SE, but directly in the application. Just call the context's Getapplicationcontext or activity's Getapplication method to get a Application object, and then do the appropriate processing.

Because the small project involves a lot of documents, here on the paste code.

Application file:

Java code:

 Public classTestApplication extends Application {Private intCurindex; Public intGetcurindex () {returnCurindex;} Public voidSetcurindex (intCurindex) { This. Curindex =Curindex;} @Override Public voidonCreate () {super.oncreate ();} @Override Public voidonterminate () {super.onterminate ();}}

There is a curindex and setter getter method in application.

Operations on application in the first acitivty:

Java code:

 This . Getapplication (); LOG.I ("data""" +Application.getcurindex ()); Application.setcurindex (5);

A second activity:

Java code:

TestApplication application = (testapplication)this. Getapplication (); LOG.I ("data""" +Application.getcurindex ()); Application.setcurindex (6);

A third activity:

Java code

 This . Getapplication (); LOG.I ("data""" "+application.getcurindex ());

In the process of operation, each time kill the corresponding activity, and then into the next activity.

Android Use Application Summary

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.