Activity management stack tool class - ActivityManager

Keywords: Android Java

Four startup modes and characteristics of Activity

  • standard: each time you start, a new instance is created and placed on top of the stack. (the Activity of this mode cannot be started with the Context of non Activity class. If the start needs to be added with the flag of "flag" Activity "new" task to create a stack).

  • singleTop (stack top reuse mode): if the Activity started is on the top of the stack, the Activity will not be rebuilt, and the onNewIntent() method of the Activity will be called. If it is not on the top of the stack, it will be in standard mode.

  • Single Task (in stack reuse mode): if the launched Activity is in the stack, a new instance will not be created and the onNewIntent() method will be called, and all activities above the Activity will be cleared (if other applications start the Activity, if there is no existing resume new Task, if there is one in the background, the background Task will also be switched to the foreground).

  • Single instance (single instance mode): a new Activity directly creates a new task stack. When the Activity of this mode exists in a stack, any later activation of the Activity will reuse the instance.


Code help class

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import java.util.Calendar;
import java.util.Stack;

public class ActManager {
    private static Stack<Activity> activityStack;
    private static ActManager instance = new ActManager();
    private long lastForwardTime = 0;
    private Activity lastAct = null;

    private ActManager() {
    }

    public static ActManager Instance() {
        return instance;
    }
    /**
     * Prevent the page from skipping repeatedly for a short time
     *
     * @return true:Can jump to false: no jump
     */
    private boolean checkForward(Context context) {
        Activity activity = null;
        boolean flag = true;
        try {
            activity = (Activity) context;
        } catch (Exception e) {
            Logger.output("Warning!!! ApplicationContext");
            //Description is ApplicationContext
        }

        long time = Calendar.getInstance().getTimeInMillis();
        if (null != activity) {
            if (null != lastAct) {
                //If it is the same activity and jumps multiple times within 150ms, multiple jumps will be blocked
                if (time - lastForwardTime < 150 && activity == lastAct) {
                    flag = false;
                }
            }
        }
        lastForwardTime = time;
        lastAct = activity;
        return flag;
    }
    /**
     * Jump to the next page
     *
     * @param packageContext context
     * @param intent intent
     */
    public void forwardActivity(Context packageContext, Intent intent) {
        if (checkForward(packageContext)) {
            packageContext.startActivity(intent);
        }
    }

    /**
     * Jump to the next page
     *
     * @param packageContext context
     * @param cls class
     */
    public void forwardActivity(Context packageContext, Class<?> cls) {
        if (checkForward(packageContext)) {
            Intent intent = new Intent(packageContext, cls);
            packageContext.startActivity(intent);
        }
    }

    /**
     * Jump to the next page and pass the String parameter
     */
    public void forwardActivity(Context packageContext, Class<?> cls, String key, String extra) {
        if (checkForward(packageContext)) {
            Intent intent = new Intent(packageContext, cls);
            intent.putExtra(key, extra);
            packageContext.startActivity(intent);
        }
    }

    /**
     * Go to the next page and pass int parameter
     */
    public void forwardActivity(Context packageContext, Class<?> cls, String key, int extra) {
        if (checkForward(packageContext)) {
            Intent intent = new Intent(packageContext, cls);
            intent.putExtra(key, extra);
            packageContext.startActivity(intent);
        }
    }

    /**
     * Jump to the next page and pass the long parameter
     */
    public void forwardActivity(Context packageContext, Class<?> cls, String key, long extra) {
        if (checkForward(packageContext)) {
            Intent intent = new Intent(packageContext, cls);
            intent.putExtra(key, extra);
            packageContext.startActivity(intent);
        }
    }

    /**
     * Jump to the next page and pass the double parameter
     */
    public void forwardActivity(Context packageContext, Class<?> cls, String key, double extra) {
        if (checkForward(packageContext)) {
            Intent intent = new Intent(packageContext, cls);
            intent.putExtra(key, extra);
            packageContext.startActivity(intent);
        }
    }

    /**
     * Jump to the next page and pass the boolean parameter
     */
    public void forwardActivity(Context packageContext, Class<?> cls, String key, boolean extra) {
        if (checkForward(packageContext)) {
            Intent intent = new Intent(packageContext, cls);
            intent.putExtra(key, extra);
            packageContext.startActivity(intent);
        }
    }

        /**
         * Put the activity into the painting stack
         *
         * @param activity act
         */
        public void pushActivity(Activity activity) {
            if (activityStack == null) {
                activityStack = new Stack<>();
            }
            if (activityStack.contains(activity)) {
                activityStack.remove(activity);
            }
            activityStack.add(activity);
        }

    /**
     * Return to previous activity
     */
    public void popActivity() {
        if (activityStack == null) {
            activityStack = new Stack<>();
            return;
        }

        if (activityStack.isEmpty()) {
            return;
        }

        Activity activity = activityStack.pop();
        if (activity != null) {
            if (!activity.isFinishing())
                activity.finish();
        }
    }

    /**
     * Return the previous activity, and the previous activity does not specify the class name activity, otherwise continue pop
     */
    public void popActivity(Class<?> cls) {
        popActivity();
        if (isCurrentActivity(cls)) {
            popActivity();
        }
    }

    /**
     * Judge whether it is the current activity according to the class name
     */
    private boolean isCurrentActivity(Class<?> cls) {
        Activity currentActivity = currentActivity();
        if (null == currentActivity) {
            return false;
        }

        if (currentActivity.getClass().equals(cls)) {
            return true;
        }
        return false;
    }

    /**
     * End Activity for specified class name
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                popActivity(activity);
            }
        }
    }

    /**
     * One activity in pop-up stack
     *
     * @param activity activity object to pop up
     */
    public void popActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }

        if (activityStack.isEmpty()) {
            return;
        }

        if (activity != null) {
            activityStack.remove(activity);
            if (!activity.isFinishing())
                activity.finish();
        }
    }

    /**
     * Get the current activity
     *
     * @return act
     */
    public Activity currentActivity() {
        if (activityStack == null || activityStack.isEmpty()) {
            return null;
        }
        return activityStack.lastElement();
    }

    /**
     * Clear the screen stack and exit the program
     */
    public void popAllActivity() {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            popActivity(activity);
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    /**
     * Clear the screen stack and enter a page
     */
    public void popAllActivityNoExit() {
        while (true) {
            Activity activityTemp = currentActivity();
            if (activityTemp == null) {
                break;
            }
            popActivity(activityTemp);
        }
    }

    /**
     * Return to the specified activity
     *
     * @param cls Specified activity
     */
    public void backToActivity(Class<?> cls) {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(cls)) {
                return;
            }
            popActivity(activity);
        }
    }

    /**
     * Pop up all screen stacks, only one activity is reserved
     *
     * @param cls activity class to keep
     */
    public void popAllActivityExceptOne(Class<?> cls) {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(cls)) {
                break;
            }
            popActivity(activity);
        }
    }

    /**
     * Determine whether the activity object exists in the screen stack
     *
     * @param activity act
     * @return Return TRUE if present, return FALSE if not present
     */
    public boolean existActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }

        return !activityStack.isEmpty() && activityStack.contains(activity);

    }

    public boolean hasOtherActivity(Activity activity) {
        if (null == activityStack || activityStack.isEmpty()) {
            return false;
        }

        if (activityStack.size() > 1) {
            return true;
        } else if (activityStack.size() == 1) {
            return !activityStack.contains(activity);
        }
        return false;
    }

}


Posted by azeem123456 on Fri, 01 May 2020 10:56:58 -0700