当前位置:网站首页>Singleton mode encapsulates activity management class

Singleton mode encapsulates activity management class

2022-07-05 10:16:00 asahi_ xin

public class AppManager {
    

    private Stack<Activity> activityStack;

    private AppManager() {
    
        activityStack = new Stack<>();
    }

    public static AppManager getInstance() {
    
        return AppHolder.appManager;
    }

    private static class AppHolder {
    
        private static AppManager appManager = new AppManager();
    }

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

    /** *  remove activity * * @param activity activity */
    public void removeActivity(Activity activity) {
    
        activityStack.remove(activity);
    }

    /** *  Get current Activity */
    private Activity currentActivity() {
    
        return activityStack.lastElement();
    }

    /** *  Remove and close activity * * @param activity activity */
    public void finishActivity(Activity activity) {
    
        removeActivity(activity);
        activity.finish();
    }

    /** *  Remove and close all activity */
    public void finishAllActivity() {
    
        for (Activity activity : activityStack) {
    
            if (null != activity) {
    
                activity.finish();
            }
        }
        activityStack.clear();
    }

    /** *  sign out  app  Called when the  */
    public void exitApp() {
    
        try {
    
            finishAllActivity();
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        } catch (Exception e) {
    
            e.printStackTrace();
        }
    }
}
原网站

版权声明
本文为[asahi_ xin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140534181787.html