Code for android to completely exit the application

Source: Internet
Author: User
Tags close page exit in


Exit method:
1) Use singleTask Loading Mode
2) define a base class for Public Functions
Define an Application

Exit form:
1) choose menu> exit
2) press the back key and prompt to exit again
3) press back to bring up the exit prompt dialog box.
4) press back to exit.


The first method is as follows: configure the loading mode singleTask of the activity in the list.

Example of a single instance in the singletask Stack: a starts B and B starts c, and click exit in menu of c to close all activities.

Add the loading mode to singletask in configuration of a. The Click Event of menu in c is to start. In method a, rewrite onNewIntent () to close page a and end the process.
@ Overrideprotected void onNewIntent (Intent intent) {// after a singletask is loaded, the onNewIntent () method is directly entered when the returned result is returned, instead of onCreate (); super. onNewIntent (intent); finish (); Process. killProcess (Process. myPid (); // process ended after all activities are closed}

Method 2

Create a Global Container arraylist <activity> in the application and add addActivity () and finishActivity () to the container ();

Public class Myapplication extends Application {public static ArrayList <Activity> activities = new ArrayList <Activity> (); public static void addActivity (Activity activity) {activities. add (activity);} public static void finishActivity () {for (int I = 0; I <activities. size (); I ++) {activities. get (I ). finish ();} Process. killProcess (Process. myPid (); // process ended after all activities are closed }}


Create a base-class BaseActivity and write the getApplication () method to reduce code duplication.) Let every activity inherit it.

public class BaseActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_base);        Myapplication app = (Myapplication) getApplication();        app.addActivity(this);    }    @Override    public boolean onCreateOptionsMenu(Menu menu)    {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.base, menu);        return true;    }}

The exit code is as follows:

1,

@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {Myapplication app = (Myapplication) getApplication (); app. finishActivity (); // The first form: Click the menu to exit, close all activities, and end the process return super. onOptionsItemSelected (item );}

2,

Int pressbacktimes = 0; private Button button; @ Override public void onBackPressed () {pressbacktimes ++; if (pressbacktimes = 1) {Toast. makeText (this, "Quit again", Toast. LENGTH_SHORT ). show (); button. postDelayed (new Runnable () {// Form 2: Click the return key twice. The toast is displayed for the first time, prompting you to press and exit again. If the interval exceeds 5 seconds, @ Override public void run () {pressbacktimes = 0 ;}, 5000) ;}else if (pressbacktimes = 2) becomes invalid) {Myapplication app = (Myapplication) getApplication (); app. finishActivity (); // click the menu to exit, close all activities, and end processes }}

3,

@ Overridepublic void onBackPressed () {// method 3: Click the return key in MainActivity. In the displayed dialog box, whether to exit new AlertDialog. builder (this ). setTitle ("title "). setMessage ("logout "). setNegativeButton ("cancel", null ). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Myapplication app = (Myapplication) getApplication (); app. finishActivity ();}}). create (). show ();}


This article is from the "wangcuijing" blog, please be sure to keep this source http://wangcuijing.blog.51cto.com/7233352/1285487

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.