Android end all Activity common methods

Problem description
When we do Android projects, sometimes we can't exit all activities when we exit the program. At this time, we need to close all activities when we exit.

Customize an Activity stack (generic method). Use a single instance Activity stack to store and manage all activities. And provides a way to exit all activities.

1. Customize an Activity stack method (general method)

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:It is used to process the exit program, which can exit all activities and write a common class
 * Detail:
 */

public class ActivityManager {

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

    private ActivityManager() {
    }

    /**
     * Get unique MyApplication instance in singleton mode
     *
     * @return
     */
    public static ActivityManager getInstance() {
        if (null == instance) {
            instance = new ActivityManager();
        }
        return instance;
    }

    /**
     * Add Activity to container
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

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

    /**
     * Traverse all activities and finish
     */
    public void exit() {
        for (Activity activity : activityList) {
            activity.finish();
        }
        System.exit(0);
    }
}

2. More comprehensive stack management class

 /**
 * Author:AND
 * Time:2018/3/16.
 * Email:2911743255@qq.com
 * Description: activity Stack Manager
 * Detail:
 */

public class ActivityStack
{
    private static ActivityStack mSingleInstance;
    private Stack<Activity> mActicityStack;

    private ActivityStack()
    {
        mActicityStack = new Stack<Activity>();
    }

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

    public Stack<Activity> getStack()
    {
        return mActicityStack;
    }

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

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

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

    /**
     * finish Specified activity
     * 
     * @author blue
     */
    public boolean finishActivity(Class<? extends Activity> actCls)
    {
        Activity act = findActivityByClass(actCls);
        if (null != act && !act.isFinishing())
        {
            act.finish();
            return true;
        }
        return false;
    }

    public Activity findActivityByClass(Class<? extends Activity> actCls)
    {
        Activity aActivity = null;
        Iterator<Activity> 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 All activities above the specified activity
     * 
     * @author blue
     */
    public boolean finishToActivity(Class<? extends Activity> actCls, boolean isIncludeSelf)
    {
        List<Activity> buf = new ArrayList<Activity>();
        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. Usage

  1. In the onCreate() method, add:
    ActivityManager.getInstance().addActivity(this);
  2. In the onDestroy() method, add:
    ActivityManager.getInstance().deleteActivity(this);
  3. Called on exit:
    ActivityManager.getInstance().exit();

Tags: Android Java

Posted on Thu, 02 Apr 2020 19:35:59 -0700 by madavies