Activity管理器

使用ActivityCollector来管理所有Activity

public class ActivityCollector {

    /** * 内部类实现单例模式 * 延迟加载,减少内存开销 * * @author xutao */
    private static class ActivityHolder {
        private static ActivityCollector instance = new ActivityCollector();
    }

    /** * 私有的构造函数 */
    private ActivityCollector() {

    }

    public static ActivityCollector getInstance() {
        return ActivityHolder.instance;
    }

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

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

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

    /** * finish所有窗口 */
    public void finishAll() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }

    /** * 返回首页 * @param MainActivityClassName */
    public void BackMainActivity(String mainActivityClassName) {
        if (StringUtil.isEmpty(mainActivityClassName)) return;
        for (Activity a : activities) {
            if (!a.isFinishing()) {
                String name = a.getClass().getName();
                if (!name.equals(mainActivityClassName)) {
                    a.finish();
                }
            }
        }
    }

    }
相关文章
相关标签/搜索