当退出应用时,结束所有activity的方法

1.自定义一个application,并将所有的activity装入activitylist里面,在 AndroidManifest.xml中注册 <application android:name=".ExitAll"></application>

package com.dragon;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.Application;

public class ExitAll extends Application {

	private List<Activity> activityList = new LinkedList<Activity>();
	private static ExitAll instance;

	private ExitAll() {
	}

	// 单例模式中获取唯一的MyApplication实例
	public static ExitAll getInstance() {
		if (null == instance) {
			instance = new ExitAll();
		}
		return instance;
	}

	// 添加Activity到容器中
	public void addActivity(Activity activity) {
		activityList.add(activity);
	}

	// 遍历所有Activity并finish
	public void exit() {
		
		
				for (Activity activity : activityList) {
					activity.finish();
					
				}
	
				System.exit(0);
			
			}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
	System.out.println("ExitAll创建");
		super.onCreate();
	}

}

2.在每一个activity加入

ExitAll.getInstance().addActivity(OneActivity.this);			//当前activity

3.在想结束的activity中加入

	ExitAll ea=ExitAll.getInstance();
		ea.addActivity(TwoActivity.this);		//当前的activity
		ea.exit();



你可能感兴趣的:(android,null,application,Class)