(1) define the stack, write an Exitactivity class, and manage the Activity in singleton mode.
The onCreate () method of the Activity calls the Exitactivity. getInstance (). addActivity (this) method.
Call Exitactivity. getInstance (). exit () to exit the application.
(2) Exitactivity class
Public class Exitactivity extends Application {private List activityList = new external List (); private static Exitactivity instance; private Exitactivity () {}// obtain the unique ExitApplication instance public static Exitactivity getInstance () {if (null = instance) {instance = new Exitactivity ();} return instance;} in singleton Mode ;} // Add the Activity to the container public void addActivity (Activity activity) {activityList. add (activity);} // traverse all activities and finish public void exit () {for (Activity: activityList) {activity. finish ();} System. exit (0 );}}
(3) three other activities
1) Implementation of activitymian
Package com. liuzuyi. exit; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {private Button next; private Button exit; int I = 0; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); next = (Button) findViewById (R. id. button2); exit = (Button) findViewById (R. id. button1); next. setOnClickListener (l); exit. setOnClickListener (ll); Exitactivity. getInstance (). addActivity (this);} OnClickListener l = new OnClickListener () {public void onClick (View v) {Intent intent = new Intent (MainActivity. this, TwoActivity. class); startActivity (intent) ;}}; OnClickListener ll = new OnClickListener () {@ Overridepublic void onClick (View v) {if (I = 0) {Toast. makeText (MainActivity. this, "the program will exit in MainActivity. Please click again", Toast. LENGTH_LONG ). show (); I = 1;} else {Exitactivity. getInstance (). exit ();}}};}
2) TwoActivity implementation
Package com. liuzuyi. exit; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class TwoActivity extends Activity {private Button next; private Button exit; int I = 0; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_two); next = (Button) findViewById (R. id. button2); exit = (Button) findViewById (R. id. button1); next. setOnClickListener (l); exit. setOnClickListener (ll); Exitactivity. getInstance (). addActivity (this);} OnClickListener l = new OnClickListener () {public void onClick (View v) {Intent intent = new Intent (TwoActivity. this, ThreeActivity. class); startActivity (intent) ;}}; OnClickListener ll = new OnClickListener () {@ Overridepublic void onClick (View v) {if (I = 0) {Toast. makeText (TwoActivity. this, "the program will exit in TwoActivity. Please click again", Toast. LENGTH_LONG ). show (); I = 1;} elseExitactivity. getInstance (). exit ();}};}
3) Implementation of ThreeActivity
Package com. liuzuyi. exit; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class ThreeActivity extends Activity {private Button next; private Button exit; int I = 0; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_three); next = (Button) findViewById (R. id. button2); exit = (Button) findViewById (R. id. button1); next. setOnClickListener (l); exit. setOnClickListener (ll); Exitactivity. getInstance (). addActivity (this);} OnClickListener l = new OnClickListener () {public void onClick (View v) {Intent intent = new Intent (ThreeActivity. this, MainActivity. class); startActivity (intent) ;}}; OnClickListener ll = new OnClickListener () {@ Overridepublic void onClick (View v) {if (I = 0) {I = 1; toast. makeText (ThreeActivity. this, "the program will exit in ThreeActivity. Please click again", Toast. LENGTH_LONG ). show ();} elseExitactivity. getInstance (). exit ();}};}
You can download the source code here:
Http://download.csdn.net/detail/liuzuyi200/7399541