欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 结束所有Activity通用方法

程序员文章站 2022-04-03 11:06:24
问题描述 做Android项目,退出程序时,有时不能退出所有的activity,这时我们就需要在退出时,关闭所有的activity。 自定义一个Activity 栈(通用方法)...

问题描述
Android项目,退出程序时,有时不能退出所有的activity,这时我们就需要在退出时,关闭所有的activity。

自定义一个Activity 栈(通用方法)。利用一个单例模式的Activity栈来存放并管理所有Activity。并提供退出所有Activity的方法。

1.自定义一个Activity 栈方法(通用方法)

package com.project.test;

import android.app.Activity;

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

/**
 * Author:AND
 * Time:2018/3/16.
 * Email:2911743255@qq.com
 * Description:用于处理退出程序时可以退出所有的activity,而编写的通用类
 * Detail:
 */

public class ActivityManager {

    private List activityList = new LinkedList();
    private static ActivityManager instance;

    private ActivityManager() {
    }

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

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

    /**
     * 移除一个activity
     *
     * @param activity
     */
    public void deleteActivity(Activity activity) {
        if (activityList != null && activityList.size() > 0) {
            if (activity != null) {
                activity.finish();
                activityList.remove(activity);
                activity = null;
            }
        }
    }

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

2.更加全面的堆栈管理类

 /**
 * Author:AND
 * Time:2018/3/16.
 * Email:2911743255@qq.com
 * Description: activity堆栈管理
 * Detail:
 */

public class ActivityStack
{
    private static ActivityStack mSingleInstance;
    private Stack mActicityStack;

    private ActivityStack()
    {
        mActicityStack = new Stack();
    }

    public static ActivityStack getInstance()
    {
        if (null == mSingleInstance)
        {
            mSingleInstance = new ActivityStack();
        }
        return mSingleInstance;
    }

    public Stack getStack()
    {
        return mActicityStack;
    }

    /**
     * 入栈
     * 
     * @author blue
     */
    public void addActivity(Activity activity)
    {
        mActicityStack.push(activity);
    }

    /**
     * 出栈
     * 
     * @author blue
     */
    public void removeActivity(Activity activity)
    {
        mActicityStack.remove(activity);
    }

    /**
     * 彻底退出
     * 
     * @author blue
     */
    public void finishAllActivity()
    {
        Activity activity;
        while (!mActicityStack.empty())
        {
            activity = mActicityStack.pop();
            if (activity != null)
                activity.finish();
        }
    }

    /**
     * finish指定的activity
     * 
     * @author blue
     */
    public boolean finishActivity(Class actCls)
    {
        Activity act = findActivityByClass(actCls);
        if (null != act && !act.isFinishing())
        {
            act.finish();
            return true;
        }
        return false;
    }

    public Activity findActivityByClass(Class actCls)
    {
        Activity aActivity = null;
        Iterator itr = mActicityStack.iterator();
        while (itr.hasNext())
        {
            aActivity = itr.next();
            if (null != aActivity && aActivity.getClass().getName().equals(actCls.getName()) && !aActivity.isFinishing())
            {
                break;
            }
            aActivity = null;
        }
        return aActivity;
    }

    /**
     * finish指定的activity之上的所有activity
     * 
     * @author blue
     */
    public boolean finishToActivity(Class actCls, boolean isIncludeSelf)
    {
        List buf = new ArrayList();
        int size = mActicityStack.size();
        Activity activity = null;
        for (int i = size - 1; i >= 0; i--)
        {
            activity = mActicityStack.get(i);
            if (activity.getClass().isAssignableFrom(actCls))
            {
                for (Activity a : buf)
                {
                    a.finish();
                }
                return true;
            } else if (i == size - 1 && isIncludeSelf)
            {
                buf.add(activity);
            } else if (i != size - 1)
            {
                buf.add(activity);
            }
        }
        return false;
    }
}

3.使用方法

在onCreate()方法里面加上:
ActivityManager.getInstance().addActivity(this); 在onDestroy()方法里面加上:
ActivityManager.getInstance().deleteActivity(this); 退出时调用:
ActivityManager.getInstance().exit();