android 完全退出应用程序
android android 退出应用程序, 单例模式管理Activity引自:android 退出应用程序会调用android.os.Process.killProcess(android.os.Process.myPid())或是System.exit(0),这只是针对第一个Activity(也就是入口的Activity)时生效。如果有A,B,C三个Activity,而想在B 或C 中Activity 退出,调用上面的方法,往往会销毁当前的Activity 返回上一个Activity。当然也可以逐个返回上一个Activity,直到跳转到入口的Activity,最后退出应用程序。但这样比较麻烦,而且逐个返回的体验并不友好。网上比较流行的方法是定义栈,写一个ExitApplication 类,利用单例模式管理Activity,在每个在Activity 的onCreate()方法中调用ExitApplication.getInstance().addActivity(this)方法,在退出时调用ExitApplication.getInstance().exit()方法,就可以完全退出应用程序了。ExitApplication 类代码如下:view plaincopy to clipboardprint?1 import java.util.LinkedList;2 import java.util.List;34 import android.app.Activity;5 import android.app.Application;67 public class ExitApplication extends Application { 9 private List<Activity> activityList=new LinkedList<Activity>();10 private static ExitApplication instance;
1112 private ExitApplication()13 { 14 }15 //单例模式中获取唯一的ExitApplication 实例16 public static ExitApplication getInstance()17 { 18 if(null == instance)19 { 20 instance = new ExitApplication();21 }22 return instance;2324 }25 //添加Activity 到容器中26 public void addActivity(Activity activity)27 { 28 activityList.add(activity);29 }30 //遍历所有Activity 并finish3132 public void exit()33 { 3435 for(Activity activity:activityList)36 { 37 activity.finish();38 }3940 System.exit(0);4142 }43 }下面的三个类IndexActivity, BActivity,CActivity 是简单的例子, 分别是IndexActivity–>BActivity–>CActivity 的跳转顺序。在每个Activity 类中onCreate()方法中调用ExitApplication.getInstance().addActivity(Activity activity)方法。在任何一个Activity 界面退出应用程序时,只要调用ExitApplication.getInstance().exit()方法,就可以在任何一个Activity中完全退出应用程序。IndexActivity 类源代码:view plaincopy to clipboardprint?44 import android.app.Activity;45 import android.content.Intent;46 import android.os.Bundle;47 import android.view.View;48 import android.view.View.OnClickListener;49 import android.widget.Button;5051 public class IndexActivity extends Activity { 52 /** Called when the activity is first created. */53 @Override54 public void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState);56 setContentView(R.layout.main);5758 Button next=(Button)findViewById(R.id.next_to_b);59 next.setOnClickListener(nextClick);6061 Button exit=(Button)findViewById(R.id.exit_main);62 exit.setOnClickListener(exitClick);63 ExitApplication.getInstance().addActivity(this);6465 }6667 OnClickListener nextClick=new OnClickListener() { 6869 @Override70 public void onClick(View v) { 71 // TODO Auto-generated method stub7273 Intent intent=new Intent(IndexActivity.this,BActivity.class);74 startActivity(intent);7576 }77 };7879 OnClickListener exitClick=new OnClickListener() { 8081 @Override82 public void onClick(View v) { 83 // TODO Auto-generated method stub84 ExitApplication.getInstance().exit();85 }86 };87 }BActivity 类源代码:view plaincopy to clipboardprint?88 import android.app.Activity;89 import android.content.Intent;90 import android.os.Bundle;91 import android.view.View;92 import android.view.View.OnClickListener;93 import android.widget.Button;9495 public class BActivity extends Activity { 9697 @Override98 protected void onCreate(Bundle savedInstanceState) { 99 // TODO Auto-generated method stub100 super.onCreate(savedInstanceState);101102 setContentView(R.layout.b);103 Button next_to_c=(Button)findViewById(R.id.next_to_c);104 next_to_c.setOnClickListener(next_to_cClick);105106 Button exit_b=(Button)findViewById(R.id.exit_b);107 exit_b.setOnClickListener(exitClick);108 ExitApplication.getInstance().addActivity(this);109110 }111112 OnClickListener next_to_cClick=new OnClickListener() { 113114 @Override115 public void onClick(View v) { 116 // TODO Auto-generated method stub117118 Intent intent=new Intent(BActivity.this,CActivity.class);119 startActivity(intent);120121 }122 };123124 OnClickListener exitClick=new OnClickListener() { 125126 @Override127 public void onClick(View v) { 128 // TODO Auto-generated method stub129 ExitApplication.getInstance().exit();130 }131 };132 }CActivity 类源代码:view plaincopy to clipboardprint?133 import android.app.Activity;134 import android.os.Bundle;135 import android.view.View;136 import android.view.View.OnClickListener;137 import android.widget.Button;138139 public class CActivity extends Activity{ 140141 @Override142 protected void onCreate(Bundle savedInstanceState) { 143 // TODO Auto-generated method stub144 super.onCreate(savedInstanceState);145146 setContentView(R.layout.c);147148 Button exit_c=(Button)findViewById(R.id.exit_c);149 exit_c.setOnClickListener(exitClick);150 ExitApplication.getInstance().addActivity(this);151152 }153154 OnClickListener exitClick=new OnClickListener() { 155156 @Override157 public void onClick(View v) { 158 // TODO Auto-generated method stub159 ExitApplication.getInstance().exit();160 //如果只是调用以下其中的一个方法,并不会完全退出应用161 //android.os.Process.killProcess(android.os.Process.myPid());162 //System.exit(0);163 }164 };165166 }