To exit multiple activities.
1. Use the List set method
Use list to save activity instances and then kill them one by one
Import java. util. using 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 extenlist <Activity> (); private static SysApplication instance; private SysApplication () {} public synchronized Static SysApplication getInstance () {if (null = instance) {instance = new SysApplication ();} return instance;} // add Activitypublic 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); // remove this} @ Overridepublic void onLowMemory () {super. onLowMemory (); System. gc () ;}} added similar code in the onCreate method of each Activity: public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); SysApplication. getInstance (). addActivity (this );}
To exit the program, call:
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.
Advantages: simple implementation and clear logic
2. Use Broadcast
Recommended !! Convenient !!
The basic logic is: Define a BaseActivity, bind broadcast to this Activity, and call finish () in the onReceive method of the broadcast; then all subsequent activities inherit this Activity, and send broadcast when exiting, exit BaseActivity. The parent activity exits and the Child activity exits.
The Code is as follows:
Import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. support. v7.app. appCompatActivity; public class BaseActivity extends AppCompatActivity {protected BroadcastReceiver broadcastReceiver = new BroadcastReceiver () {@ Override public void onReceive (Context context, Intent intent) {finish ();}}; @ Override public void onResume () {super. onResume (); // register the broadcast IntentFilter filter = new IntentFilter (); filter in the current activity. addAction ("ExitApp"); this. registerReceiver (this. broadcastReceiver, filter) ;}@ Override protected void onDestroy () {// TODO Auto-generated method stub super. onDestroy (); // unbind this. unregisterReceiver (this. broadcastReceiver );}}
An Activity instance:
Import android. content. intent; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. util. log; import android. view. keyEvent; import android. view. view; import android. widget. button; import android. widget. toast; public class ActivityC extends BaseActivity {Button btn; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_activity_c); SysApplication. getInstance (). addActivity (this); btn = (Button) findViewById (R. id. acticityc_btn); // close all activity btn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {}});}/*** capture the mobile phone physical menu key */private long exitTime = 0; @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) {// & event. getAction () = KeyEvent. ACTION_DOWN if (System. currentTimeMillis ()-exitTime)> 2000) {Toast. makeText (getApplicationContext (), "exit the program again", Toast. LENGTH_SHORT ). show (); exitTime = System. currentTimeMillis () ;}else {myExit () ;}return true ;}return super. onKeyDown (keyCode, event);} protected void myExit () {// second exit method // SysApplication. getInstance (). exit (); Intent intent = new Intent (); intent. setAction ("ExitApp"); this. sendBroadcast (intent); // super. finish () ;}@ Override protected void onStop () {super. onStop (); Log. I ("tag", "ActivityC-onStop") ;}@ Override protected void onDestroy () {super. onDestroy (); Log. I ("tag", "ActivityC-onDestroy ");}}
This is called at the exit:
Private long exitTime = 0; @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) {// & event. getAction () = KeyEvent. ACTION_DOWN if (System. currentTimeMillis ()-exitTime)> 2000) {Toast. makeText (getApplicationContext (), "exit the program again", Toast. LENGTH_SHORT ). show (); exitTime = System. currentTimeMillis () ;}else {myExit () ;}return true ;}return super. onKeyDown (keyCode, event);} protected void myExit () {// second exit method // SysApplication. getInstance (). exit (); Intent intent = new Intent (); intent. setAction ("ExitApp"); this. sendBroadcast (intent); // super. finish ();}
Advantages: Convenient and convenient !! You only need to send a broadcast at the exit !!
This is the output when I exit:
08-09 15:33:48.869 26919-26919/example.com.closeapp I/tag: MainActivity-onPause08-09 15:33:49.279 26919-26919/example.com.closeapp I/tag: MainActivity-onStop08-09 15:33:51.569 26919-26959/example.com.closeapp D/OpenGLRenderer: endAllStagingAnimators on 0x55850598b0 (RippleDrawable) with handle 0x558504151008-09 15:33:56.079 26919-26919/example.com.closeapp I/tag: MainActivity-onDestroy08-09 15:33:56.089 26919-26919/example.com.closeapp I/tag: ActivityB-onDestroy08-09 15:33:56.399 26919-26919/example.com.closeapp I/tag: ActivityC-onDestroy