退出应用 关闭多个Activity
Activity3
public class Activity3 extends ListActivity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.i("bqt", "【Activity3】-onCreate");String[] array = { "设置Activity的launchMod为singleTask,此时不需要设置FLAG,适用于返回到主界面,注意不会调用其onCreate方法", //"设置FLAG_ACTIVITY_CLEAR_TOP,可清除顶部Activity,注意新启动的Activity是重建的", //"通过堆栈管理器去管理",//"使用广播机制解决",//"通过Activity跳转中传递requestCode的之后根据onActivityResult来遍历关闭", };for (int i = 0; i < array.length; i++) {array[i] = i + "、" + array[i];}setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {switch (position) {case 0:startActivity(new Intent(this, MainActivity.class));//会先后把Activity2和Activity3干掉(调用其onDestroy方法),MainActivity不会重建(不调用其onCreate方法)break;case 1:Intent intent = new Intent(this, Activity2.class);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//会先后把Activity2和Activity3干掉,然后【重建】Activity2(一定注意,新启动的Activity2是重建的)startActivity(intent);break;case 2:MyActivityManager.getInstance().push(this);//实际肯定是在onCreate时就加到栈中的MyActivityManager.getInstance().finishAllActivity();break;case 3:IntentFilter filter = new IntentFilter();filter.addAction(INIENT_FINISH);registerReceiver(mFinishReceiver, filter);sendBroadcast(new Intent(INIENT_FINISH));break;case 4:setResult(FINISH_RESULTCODE);finish();break;case 5:break;}}//下面这些本来是要抽取到BaseActivity中的//******************************************************************************************@Overrideprotected void onDestroy() {super.onDestroy();Log.i("bqt", "【Activity3】-onDestroy");try {if (mFinishReceiver != null) unregisterReceiver(mFinishReceiver);//多次取消或在没有注册时取消会报IllegalArgumentException} catch (Exception e) {}}/** 退出广播对应的ACTION*/public final static String INIENT_FINISH = "com.bqt.broadcast.finish";/**监听退出的广播 */public BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if (INIENT_FINISH.equals(intent.getAction())) finish();}};//******************************************************************************************/** 关闭时候的requestCode请求码 */public final static int FINISH_REQUESTCODE = 20094;/** 关闭时候的resultCode请求码 */public final static int FINISH_RESULTCODE = 20094;@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == FINISH_REQUESTCODE && resultCode == FINISH_RESULTCODE) {setResult(FINISH_RESULTCODE);finish();}super.onActivityResult(requestCode, resultCode, data);}}
Activity2
public class Activity2 extends ListActivity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.i("bqt", "【Activity2】-onCreate");String[] array = { "采用任务栈方式退出整个程序", //"通过堆栈管理器去管理",//"使用广播机制解决",//"通过Activity跳转中传递requestCode的之后根据onActivityResult来遍历关闭", };for (int i = 0; i < array.length; i++) {array[i] = i + "、" + array[i];}setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {switch (position) {case 0:startActivity(new Intent(this, Activity3.class));break;case 1:MyActivityManager.getInstance().push(this);//实际肯定是在onCreate时就加到栈中的startActivity(new Intent(this, Activity3.class));break;case 2:IntentFilter filter = new IntentFilter();filter.addAction(INIENT_FINISH);registerReceiver(mFinishReceiver, filter);startActivity(new Intent(this, Activity3.class));break;case 3:startActivityForResult(new Intent(getApplicationContext(), Activity3.class), FINISH_REQUESTCODE);break;}}//下面这些本来是要抽取到BaseActivity中的//******************************************************************************************@Overrideprotected void onDestroy() {super.onDestroy();Log.i("bqt", "【Activity2】-onDestroy");try {if (mFinishReceiver != null) unregisterReceiver(mFinishReceiver);//多次取消或在没有注册时取消会报IllegalArgumentException} catch (Exception e) {}}/** 退出广播对应的ACTION*/public final static String INIENT_FINISH = "com.bqt.broadcast.finish";/**监听退出的广播 */public BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if (INIENT_FINISH.equals(intent.getAction())) finish();}};//******************************************************************************************/** 关闭时候的requestCode请求码 */public final static int FINISH_REQUESTCODE = 20094;/** 关闭时候的resultCode请求码 */public final static int FINISH_RESULTCODE = 20094;@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == FINISH_REQUESTCODE && resultCode == FINISH_RESULTCODE) {setResult(FINISH_RESULTCODE);finish();}super.onActivityResult(requestCode, resultCode, data);}}
MainActivity
public class MainActivity extends ListActivity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.i("bqt", "【MainActivity】-onCreate");String[] array = { "采用任务栈方式退出整个程序", //"通过堆栈管理器去管理",//"使用广播机制解决",//"通过Activity跳转返回后根据onActivityResult来遍历关闭", //"其他不合适的方式 "};for (int i = 0; i < array.length; i++) {array[i] = i + "、" + array[i];}setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {switch (position) {case 0:startActivity(new Intent(this, Activity2.class));break;case 1:// MyActivityManager.getInstance().push(this);//实际肯定是在onCreate时就加到栈中的startActivity(new Intent(this, Activity2.class));break;case 2:// IntentFilter filter = new IntentFilter();// filter.addAction(INIENT_FINISH);// registerReceiver(mFinishReceiver, filter);startActivity(new Intent(this, Activity2.class));break;case 3:// startActivityForResult(new Intent(getApplicationContext(), Activity2.class), FINISH_REQUESTCODE);startActivity(new Intent(this, Activity2.class));break;case 4:startActivity(new Intent(this, ActivityKill.class));MyActivityManager.getInstance().push(this);break;}}//下面这些本来是要抽取到BaseActivity中的//******************************************************************************************@Overrideprotected void onDestroy() {super.onDestroy();Log.i("bqt", "【MainActivity】-onDestroy");try {if (mFinishReceiver != null) unregisterReceiver(mFinishReceiver);//多次取消或在没有注册时取消会报IllegalArgumentException} catch (Exception e) {}}/** 退出广播对应的ACTION*/public final static String INIENT_FINISH = "com.bqt.broadcast.finish";/**监听退出的广播 */public BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if (INIENT_FINISH.equals(intent.getAction())) finish();}};//******************************************************************************************/** 关闭时候的requestCode请求码 */public final static int FINISH_REQUESTCODE = 20094;/** 关闭时候的resultCode请求码 */public final static int FINISH_RESULTCODE = 20094;@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == FINISH_REQUESTCODE && resultCode == FINISH_RESULTCODE) {finish();}super.onActivityResult(requestCode, resultCode, data);}}
ActivityKill
public class ActivityKill extends ListActivity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.i("bqt", "ActivityKill-onCreate");String[] array = { "杀死自己-不会调用onDestroy方法", //"干掉虚拟机-不会调用onDestroy方法", //"杀死空进程、后台进程,不能杀自己", //"将本Activity添加到Stack中", //"获取所有保存的Stack",//"将本Activity从Stack中移除-会调用finish方法", };setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {switch (position) {case 0:android.os.Process.killProcess(android.os.Process.myPid());//自杀,只能杀死自己的进程。杀死后会立即重启break;case 1:System.exit(0);//杀死后会立即重启break;case 2:ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);//获取系统的进程管理器服务,必须加权限【KILL_BACKGROUND_PROCESSES】am.killBackgroundProcesses(getPackageName());//只能杀死【空进程和后台进程】,【服务进程和前台进程】及【系统核心进程】不能杀死,也【不能杀自己】break;case 3:MyActivityManager.getInstance().push(this);break;case 4:List<String> list = MyActivityManager.getInstance().getActivityNameList();if (list != null && list.size() > 0) Toast.makeText(this, "所有的Activity:" + list.toString(), Toast.LENGTH_SHORT).show();else Toast.makeText(this, "Stack中没有Activity", Toast.LENGTH_SHORT).show();break;case 5:MyActivityManager.getInstance().pop(this);break;}}@Overrideprotected void onDestroy() {super.onDestroy();Log.i("bqt", "【ActivityKill】-onDestroy");}}
Activity管理器
public class MyActivityManager {private static MyActivityManager instance;private Stack<Activity> activityStack; //用于管理activityStack的栈private MyActivityManager() {}//单例模式public static MyActivityManager getInstance() {if (instance == null) instance = new MyActivityManager();return instance;}//入栈public void push(Activity activity) {if (activityStack == null) activityStack = new Stack<Activity>();//******************************************************************************************for (int i = 0; i < activityStack.size(); i++) {if (activity == activityStack.get(i)) return;}//******************************************************************************************activityStack.add(activity);}//出栈public void pop(Activity activity) {if (activityStack != null && activityStack.size() > 0 && activity != null) {activity.finish();activityStack.remove(activity);activity = null;}}//获取栈顶的activity,先进后出原则public Activity getLastActivity() {if (activityStack == null || activityStack.size() == 0) return null;return activityStack.lastElement();}//退出所有activitypublic void finishAllActivity() {if (activityStack != null) {while (activityStack.size() > 0) {Activity activity = getLastActivity();if (activity == null) break;pop(activity);}}}//获取Stackpublic Stack<Activity> getActivityStack() {return activityStack;}//获取Stack中元素的名称(含包名)public List<String> getActivityNameList() {if (activityStack != null) {List<String> list = new ArrayList<String>();for (int i = 0; i < activityStack.size(); i++) {Activity activity = activityStack.get(i);if (activity != null) list.add(activity.getClass().getName());}return list;} else return null;}}
附件列表
退出应用 关闭多个Activity的更多相关文章
- Android退出时关闭所有Activity的方法
Android退出时,有的Activity可能没有被关闭.为了在Android退出时关闭所有的Activity,设计了以下的类: //关闭Activity的类 public class CloseAc ...
- 如何退出调起多个Activity的Application?
1.记录打开的Activity 每打开一个activity,即记录下来,需要关闭时,关闭每一个activity即可. 2.发送特定的广播 在需要结束应用时,发送一个特定广播,每个activity收到此 ...
- 【Android】退出运行了多个Activity的应用
日常开发APP的时候,绝大多数情况下会有多个Activity,而finish()方法只能结束掉一个Activity,那么,我们可以通过什么方法去退出整个Application呢? 根据各大牛的经验,有 ...
- 如何退出Activity?如何安全退出已调用多个Activity的Application?
如何退出Activity?如何安全退出已调用多个Activity的Application? 退出Activity直接调用finish()方法 //用户点击back键就是退出一个Activity 退出 ...
- 如何安全退出已调用多个Activity的Application?
如何退出Activity?如何安全退出已调用多个Activity的Application? 退出Activity直接调用finish()方法 //用户点击back键就是退出一个Activity 退出 ...
- 如何退出Activity?如何安全退出已调用多个Activity的Application?
对于单一Activity的应用来说,退出很简单,直接finish()即可. 1.抛异常强制退出: 该方法通过抛异常,使程序ForceClose. 验证可以,但是,需要解决的问题是,如何使程序结束掉,而 ...
- 如何安全退出已调用多个Activity的应用
对于单一Activity的应用来说,退出很简单,直接finish()即可.当然,也可以用killProcess()和System.exit()这样的方法. 但是,对于多Activity的应用来说,在打 ...
- winform退出或关闭窗体时弹窗提示代码:转
winform退出或关闭窗体时弹窗提示代码,当我们点击窗体的 X 按钮时,会弹出一个对话框,询问我们是直接退出,还是最小化到托盘,还是取消这个行为.或是是否保存当前修改等等.以下以最小化到托盘为例. ...
- 一次性关闭所有的Activity
原文:一次性关闭所有的Activity 一次性关闭所有的Activity ActivityManager am = (ActivityManager)getSystemService (Context ...
随机推荐
- Android源码下载方法详解
转自:http://www.cnblogs.com/anakin/archive/2011/12/20/2295276.html Android源码下载方法详解 相信很多下载过内核的人都对这个很熟悉 ...
- JS form表单提交的方法
1.当输入用户名和密码为空的时候,需要判断.这时候就用到了校验用户名和密码,这个需要在jsp的前端页面写:有两种方法,一种是用submit提交.一种是用button提交.方法一: 在jsp的前端页面的 ...
- 浅谈intval()函数用法
<? } } 总结:intval()函数功能1.参数一定是数字否则会报错,2.如果是数字那一定是整数,如果有小点,那会省略掉,3,强调参数可以有“-”值.4.参数第一位不应为0开头,不然会自动转 ...
- Delphi-Concat 函数
函数名称 Concat 所在单元 System 函数原型 function Concat ( const String1 {,String2 ...} : string ) : string; 函数功 ...
- 常用命令(ubuntu)
1.打开终端的方法 Ubuntu 中按左侧栏的第一个“面板主页(Dash 主页)”(可以按win键调出),在里面输入terminal可以打开终端,另外打开终端的快捷键是Ctrl+Alt+T 2.修改用 ...
- JS对象与json字符串格式
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...
- 未能写入输出文件 c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary
ERROR: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/root/.... 一般遇到 ...
- SVN客户端忽略无关文件-备
修改前请先备份文件 ~/.subversion/config. 1,打开Terminal,输入命令: $ open ~/.subversion/config 2,在打开的文件中寻找:`global ...
- JDBC操作TimesTen
无论是操作本地的还是远程的TimesTen服务,客户端都需要安装Tiems Client,并配置客户端DSN. 基本信息如下: 1:创建客户端DSN 点击“添加”: 双击“TimesTen Clien ...
- 001Spark文件分析测试
使用spark-1.4.1-bin-hadoop2.6进行处理,测试文件大小为3G, 测试结果: 1:统计一个文件中某个字符的个数 scala> sc.textFile("/home/ ...