Android app exit Method 1: click twice to exit the app

Source: Internet
Author: User

(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


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.