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"