Android platform completely exits the application

Source: Internet
Author: User

Android will call android when exiting the application. OS. process. killProcess (android. OS. process. myPid () or System. exit (0), which is only effective for the first Activity (that is, the Activity of the entry. If there are three activities A, B, and C, and you want to exit the Activity in B or C, calling the above method will usually destroy the current Activity and return the previous Activity. Of course, you can also return the previous Activity one by one until you jump to the Activity at the entry, and finally exit the application. However, this is troublesome and the return experience is unfriendly.

The most popular method on the Internet is to define the stack, write an ExitApplication class, manage the Activity in singleton mode, and call ExitApplication in each onCreate () method of the Activity. getInstance (). addActivity (this) method, which calls ExitApplication when exiting. getInstance (). exit () method to completely exit the application.

Import java. util. using list; import java. util. list; import android. app. activity; import android. app. application; public class ExitApplication extends Application {private List activityList = new extends List (); private static ExitApplication instance; private ExitApplication () {}// obtain the unique ExitApplication instance public static ExitApplication getInstance () {if (null = instance) {instance = new ExitApplication ();} return instance;} in singleton Mode ;} // Add the Activity to the container public void addActivity (Activity activity) {activityList. add (activity);} // traverse all activities and finishpublic void exit () {for (Activity: activityList) {activity. finish ();} System. exit (0 );}}

The following three types of IndexActivity, BActivity, and CActivity are simple examples of the jump sequence of IndexActivity-> BActivity-> CActivity. Call the ExitApplication. getInstance (). addActivity (Activity) method in the onCreate () method of each activity class. When you exit an application on any Activity interface, you can completely exit the application in any Activity by calling the ExitApplication. getInstance (). exit () method.

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;public class IndexActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button next=(Button)findViewById(R.id.next_to_b);        next.setOnClickListener(nextClick);        Button exit=(Button)findViewById(R.id.exit_main);        exit.setOnClickListener(exitClick);        ExitApplication.getInstance().addActivity(this);    }    OnClickListener nextClick=new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent=new Intent(IndexActivity.this,BActivity.class);startActivity(intent);}};    OnClickListener exitClick=new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubExitApplication.getInstance().exit();}};}

CActivity source code:

Import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class CActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. c); Button exit_c = (Button) findViewById (R. id. exit_c); exit_c.setOnClickListener (exitClick); ExitApplication. getInstance (). addActivity (this);} OnClickListener exitClick = new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubExitApplication. getInstance (). exit (); // If you only call one of the following methods, the application will not be completely exited. // android. OS. process. killProcess (android. OS. process. myPid (); // System. exit (0 );}};}

 

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.