The role of Android note--application

Source: Internet
Author: User

First, what is application?

Application and Actovotu,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 . Usually we do not need to specify a application, then the system will automatically help us to create, if you need to create their own application, it is easy to create a class to inherit application and register in manifest application tag (just add a name attribute to the application tag to set the name of your application).

The Android system 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 . And 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 activity,service are the same object. So through the application to do some, data transfer , data sharing , data caching and other operations.

Second, application configuration 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

Third, the entry point of Android program

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

? Android.content.Context
? Android.content.ContextWrapper

The 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.

Iv. overriding the life cycle events of the application class

Public void onCreate()

Called when the application is created, this method can be implemented to create and instantiate any application state variables or shared resources . You can also get a single case of application in this method.

  public void onterminate ()

  Called when the Application object is terminated , it is not guaranteed to be called , and when the program is terminated by the kernel to release resources for other applications, it will not be alerted and does not call The Onterminate method of an application's object terminates the process directly.

  public void onlowmemory()

When system resources are scarce , we can release additional memory here, which is usually done only when the background process has ended, but the foreground application is still missing memory when it is called. You can override this method to empty the cache or release unnecessary resources .

  public void Ontrimmemory(Int. level)

Called when the runtime determines that the current application should reduce its memory overhead (usually when running into the background), including a level parameter that provides the context for the request.

  public void onconfigurationchanged (Configuration newconfig)

Unlike Activity, when configuration changes, application objects are not terminated and restarted. If the values used by the application depend on a specific configuration, override this method to load these values, or change the configuration values at the application level.

v. Data transmission     

If you have an activity a, jump to activity B, and need to pass some data, the usual practice is to Intent.putextra () let Intent carry it, or have a bundle to add information to the bundle to let Intent pass the bundle object To implement delivery. But the problem with this is that the data types that Intent and bundles can carry are basic data types that are cumbersome to implement for complex data transfer, and often need to implement Serializable or parcellable interfaces. This is actually an IPC data transfer method for Android. If the two of our activity is in the same process, why bother, just pass the reference to the object that needs to pass through.

The basic idea is to create a HashMap in application, with a string of key,object as value so that our HashMap can store any type of object. In activity A, you put the object that needs to be passed in the HashMap, and then pass the key to activity B through Intent or other means, activity B can take the object out of the HashMap based on the string. As long as the downward transformation, the implementation of the object delivery.

vi. data sharing

Define two and, the code is the same

1 TextView TV;2 EditText et;3 4 @Override5 protected voidonCreate (Bundle savedinstancestate) {6     Super. OnCreate (savedinstancestate);7     //Setcontentview (r.layout.activity_main_activity2);8 Setcontentview (r.layout.activity_main2);9 TenTV =(TextView) Findviewbyid (; OneET =(EditText) Findviewbyid (; A      -Tv.settext ("Shared data:" +Getapp (). Gettextdata ()); -      theFindviewbyid ( Setonclicklistener (NewOnclicklistener () { - @Override -          Public voidOnClick (View v) { - Getapp (). Settextdata (Et.gettext (). toString ()); +Tv.settext ("Shared data:" +Et.gettext (). toString ()); -         } +     }); A } at  -  PublicApp Getapp () { -     return( App) Getapplicationcontext ()); -}

Define a class app that integrates application

1  Public classAppextendsApplication {2     3     PrivateString textData = "Default";4     5      Public voidSettextdata (String textData) {6          This. TextData =TextData;7     }8      PublicString Gettextdata () {9         returnTextData;Ten     } One}

Modify the Androidmanifest.xml file, using a custom application, two activity is the entrance:

1<Application2Android:name= "COM.CARLOZ.LEARNAPPLICATION.APP"3Android:allowbackup= "true"4android:icon= "@drawable/ic_launcher"5Android:label= "@string/app_name"6Android:theme= "@style/apptheme" >7<Activity8Android:name= ". Mainactivity "9Android:label= "@string/app_name" >Ten<intent-filter> One<action android:name= "Android.intent.action.MAIN"/> A  -<category android:name= "Android.intent.category.LAUNCHER"/> -</intent-filter> the</activity> -<Activity -Android:name= "Com.carloz.learnapplication.MainActivity2" -Android:label= "@string/title_activity_main_activity2" > +<intent-filter> -<action android:name= "Android.intent.action.MAIN"/> +  A<category android:name= "Android.intent.category.LAUNCHER"/> at</intent-filter> -</activity> -</application>

As a result, the data in one activity modifies the app, and in another activity can be obtained

vii. data caching

I usually get used to building two application in HashMap one for data transfer (see three), One for caching some data. For example, there is an activity that needs to get some data from the website, and then we can save this data to application  rom on or SDcard

The role of Android note--application

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: 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.