Application global variables in Android & the problem that you cannot exit when using the tab page

Source: Internet
Author: User

 

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 the application.

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 callGetapplicationcontext of Context
OrGetapplication of activityMethod to obtain an application object and then process it accordingly.

Exit button implementation

First, create an application to store the list of all opened activities. The Code is as follows:

Package com. deaboway. view;

Import java. util. arraylist;

Import java. util. List;

Import Android. App. activity;

Import Android. App. Application;

Public
Class deaboway
Extends application {

Private list <activity> mainactivity
= New arraylist <activity> ();

Public list <activity> mainactivity (){

Return mainactivity;

}
Public
Void addactivity (Activity Act ){

Mainactivity. Add (Act );
}
Public
Void finishall (){

For (Activity Act: mainactivity ){

If (! Act. isfinishing ()){

Act. Finish ();
}
}
Mainactivity =
NULL;
}
}

Next, add the following to the @ override public void oncreate (bundle savedinstancestate) {} method of all activities:

Public
Void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Deaboway appstate = (deaboway) This. getapplication ();

Appstate. addactivity (this );

Setcontentview (R. layout. Main );
.......
}

Third, call the exit button:

Onclicklistener (){
Public
Void onclick (view v ){

Deaboway appstate = (deaboway) getapplicationcontext ();

Appstate. finishall ();
}
}

Finally, remember to add Android: Name = ". deaboway" to the application tag of manifest"

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.