Android关闭多个activity
2017-02-04 16:21
197 查看
一、使用集合
先创建一个用于添加activity和销毁activity的集合类:
public class SysApplication extends Application {
private List<Activity> mList = new LinkedList<Activity>();
private static SysApplication instance;
private SysApplication() {
}
public synchronized static SysApplication getInstance() {
if (null == instance) {
instance = new SysApplication();
}
return instance;
}
// add Activity
public void addActivity(Activity activity) {
mList.add(activity);
}
public void exit() {
try {
for (Activity activity : mList) {
if (activity != null)
activity.finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//注意:次方法用于垃圾回收,如果手机内存小,或使用虚拟机测试,一定要注释掉这段代码
@Override
public void onLowMemory() {
super.onLowMemory();
System.gc();
}
}
在需要集体销毁的Activity的onCreate方法中添加如下代码:
在需要退出的地方添加如下代码:
二、使用栈
public class ActivityCollector {
private static ActivityCollector instance;
private Stack<Activity> activityStack;//activity栈
private ActivityCollector() {
}
//单例模式
public static ActivityCollector getInstance() {
if (instance == null) {
instance = new ActivityCollector();
}
return instance;
}
/**
* 清空stack
*/
public void clearStack(){
if(activityStack != null){
activityStack.clear();
}
}
//把一个activity压入栈中
public void pushOneActivity(Activity actvity) {
if (activityStack == null) {
activityStack = new Stack<Activity>();
}
activityStack.add(actvity);
Log.d("MyActivityManager ", "size = " + activityStack.size());
}
//获取栈顶的activity,先进后出原则
public Activity getLastActivity() {
return activityStack.lastElement();
}
//移除一个activity
public void popOneActivity(Activity activity) {
if (activityStack != null && activityStack.size() > 0) {
if (activity != null) {
activity.finish();
activityStack.remove(activity);
activity = null;
}
}
}
//退出所有activity
public void finishAllActivity() {
if (activityStack != null) {
while (activityStack.size() > 0) {
Activity activity = getLastActivity();
if (activity == null) break;
popOneActivity(activity);
}
}
}
}
使用方法和第一种类似。
先创建一个用于添加activity和销毁activity的集合类:
public class SysApplication extends Application {
private List<Activity> mList = new LinkedList<Activity>();
private static SysApplication instance;
private SysApplication() {
}
public synchronized static SysApplication getInstance() {
if (null == instance) {
instance = new SysApplication();
}
return instance;
}
// add Activity
public void addActivity(Activity activity) {
mList.add(activity);
}
public void exit() {
try {
for (Activity activity : mList) {
if (activity != null)
activity.finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//注意:次方法用于垃圾回收,如果手机内存小,或使用虚拟机测试,一定要注释掉这段代码
@Override
public void onLowMemory() {
super.onLowMemory();
System.gc();
}
}
在需要集体销毁的Activity的onCreate方法中添加如下代码:
SysApplication.getInstance().addActivity(this);
在需要退出的地方添加如下代码:
SysApplication.getInstance().exit();
二、使用栈
public class ActivityCollector {
private static ActivityCollector instance;
private Stack<Activity> activityStack;//activity栈
private ActivityCollector() {
}
//单例模式
public static ActivityCollector getInstance() {
if (instance == null) {
instance = new ActivityCollector();
}
return instance;
}
/**
* 清空stack
*/
public void clearStack(){
if(activityStack != null){
activityStack.clear();
}
}
//把一个activity压入栈中
public void pushOneActivity(Activity actvity) {
if (activityStack == null) {
activityStack = new Stack<Activity>();
}
activityStack.add(actvity);
Log.d("MyActivityManager ", "size = " + activityStack.size());
}
//获取栈顶的activity,先进后出原则
public Activity getLastActivity() {
return activityStack.lastElement();
}
//移除一个activity
public void popOneActivity(Activity activity) {
if (activityStack != null && activityStack.size() > 0) {
if (activity != null) {
activity.finish();
activityStack.remove(activity);
activity = null;
}
}
}
//退出所有activity
public void finishAllActivity() {
if (activityStack != null) {
while (activityStack.size() > 0) {
Activity activity = getLastActivity();
if (activity == null) break;
popOneActivity(activity);
}
}
}
}
使用方法和第一种类似。
相关文章推荐
- android程序退出关闭多个activity
- android 退出时 同时关闭多个activity
- Android中同时关闭多个Activity
- android 如何在activity中控制屏幕点亮与关闭
- Android 2.2关闭所有Activity完全退出程序方法
- android 之得到新打开Activity 关闭后返回的数据
- Android中包含多个Activity的应用退出按钮实现
- Android中包含多个Activity的应用退出按钮实现
- android开发中,按手机返回键,当前Activity 和 ProgressDialog 同时关闭?
- 初学Android,启动,关闭Activity(七)
- 关闭android程序里所有的activity
- Android学习札记40:关于安全退出已创建多个Activity的应用(3)
- Android判断网络状态是否断开+Android完全关闭应用程序+ 本文讲述了Android中不同Activity之间的数据传递 — Bundle对象的实现(-)+Android中Bundle的使用示例
- Android Service如何关闭Activity
- Android 在Activity中关闭另一个Activity
- 关闭多个activity
- Android中包含多个Activity的应用退出按钮实现
- Android网络状态及关闭所有Activity
- 9.1、Android中得到新打开Activity 关闭后返回的数据
- Android学习札记39:关于安全退出已创建多个Activity的应用(2)