How does Android exit the entire application?

Source: Internet
Author: User

When developing the andorid program, it will inevitably involve exiting the entire application. How can we completely exit? Refer to the following key code:


[Java] import java. util. Collections list;
Import java. util. List;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. Application;
Import android. content. DialogInterface;
Import android. content. Intent;
 
Public class SysApplication extends Application {
Private List <Activity> mList = new Entity List <Activity> ();
Private static SysApplication instance;
 
Private SysApplication (){
}
 
Public synchronized static SysApplication getInstance (){
If (null = instance ){
Instance = new SysApplication ();
}
Return instance;
}
 
// Add Activity
Public void addActivity (Activity activity ){
MList. add (activity );
}
 
Public void exit (){
Try {
For (Activity activity: mList ){
If (activity! = Null)
Activity. finish ();
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
System. exit (0 );
}
}
 
@ Override
Public void onLowMemory (){
Super. onLowMemory ();
System. gc ();
}
 
}
Import java. util. Collections list;
Import java. util. List;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. Application;
Import android. content. DialogInterface;
Import android. content. Intent;

Public class SysApplication extends Application {
Private List <Activity> mList = new Entity List <Activity> ();
Private static SysApplication instance;

Private SysApplication (){
}

Public synchronized static SysApplication getInstance (){
If (null = instance ){
Instance = new SysApplication ();
}
Return instance;
}

// Add Activity
Public void addActivity (Activity activity ){
MList. add (activity );
}

Public void exit (){
Try {
For (Activity activity: mList ){
If (activity! = Null)
Activity. finish ();
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
System. exit (0 );
}
}

@ Override
Public void onLowMemory (){
Super. onLowMemory ();
System. gc ();
}

} Add similar code in the onCreate method of each Activity:


[Java] public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SysApplication. getInstance (). addActivity (this );
}
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SysApplication. getInstance (). addActivity (this );
}


To exit the program, call:


[Java] SysApplication. getInstance (). exit ();
SysApplication. getInstance (). exit ();


In short, you can add reference of each Activity to a global linked list in singleton mode. Each time you exit a program and call System. exit (0), you must first call the finish method of the Activity in the linked list.

From the pure soul

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.