Android activity管理

遇到的问题:登录界面(activity)--(finish)-->A(activity)-------->B(activity)------(finish)------->登录界面(activity)

在B界面退出登录后跳转到登录界面,再按返回键,我想要的是直接退出程序,可是现在是返回到A界面。


解决方法:

写一个Activity管理类

代码:

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

public class ActivityManager {

	private static List<Activity> activities = new ArrayList<Activity>();

	public static void addActivity(Activity activity) {
		activities.add(activity);
	}

	public static void removeActivity(Activity activity) {
		activities.remove(activity);
	}

	public static void finishAll() {
		for (Activity activity : activities) {
			if (!activity.isFinishing()) {
				activity.finish();
			}
		}
	}

}


Activity类中:

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		ActivityManager.addActivity(this);
		


@Override
	protected void onDestroy() {
		ActivityManager.addActivity(this);
		super.onDestroy();
	}


当我对出登录时,调用finishAll()方法

你可能感兴趣的:(Android activity管理)