退出应用 关闭多个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 ...
随机推荐
- 【BZOJ1010】玩具装箱
Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...
- javescript扩展方法
<script type="text/javascript"> //扩展方法 '原型'->'prototype' //通过类对像的prototype设置扩展方法 ...
- MongoDB-固定集合 capped collection 操作 介绍
固定集合:capped collection 是性能出色的固定大小的集合,以LRU算法淘汰记录,自助维护集合中的对象的插入顺序,创建时预先制定大小,空间使用完,心对象取代旧的对象,保持最新的数据. 可 ...
- bzoj1115: [POI2009]石子游戏Kam
Description 有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子,但是要保证操作后仍然满足初始时的条件谁没有石子可移时输掉游戏 ...
- SANS top 20
What Are the Controls?The detailed Consensus Audit Guidelines are posted at http://www.sans.org/cag/ ...
- Visual Studio如何删除多余的空行
原文:Visual Studio如何删除多余的空行 如何在Visual Studio中删除多余的空格: 适用于:Visual Studio2008 &2010 1. Ctrl + ...
- 生产环境下,不建议用早期版本的Mono........
一个BUG修复需要4年!!!!!!!!!!!!!也是醉了....不造被微软收购后的Xamarin会不会好起来
- java安全HTTPS工具类
import java.io.FileInputStream; import java.security.KeyStore; import java.security.SecureRandom; im ...
- Linux下Apache PHP MYSQL 默认安装路径
apache: 如果采用RPM包安装,安装路径应在 /etc/httpd目录下apache配置文件:/etc/httpd/conf/httpd.confApache模块路径:/usr/sbin/apa ...
- Android Studio开发环境部署
Step0:背景,那些年-- Step1:安装JDK Step2:安装Android Studio 其他问题1:安装Android Studio之前没有先安装JDK 其他问题2:No JVM inst ...