美文网首页
Android工作整理-Activity管理,包括退出登录

Android工作整理-Activity管理,包括退出登录

作者: zhongcx | 来源:发表于2020-07-16 10:32 被阅读0次
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Handler;
    import android.text.TextUtils;
    import android.view.View;
    import android.view.inputmethod.InputMethodManager;
    
    import java.util.Stack;
    
    /**
     * app页面管理
     * 使用:
     * onCreate()-->MyAppManager.getInstance().addActivity(this);
     * onDestroy()-->MyAppManager.getInstance().removeActivity(this);
     * 退出--> MyAppManager.getInstance().exit();
     */
    public class MyAppManager {
        private Stack<Activity> mActivities = new Stack<>();
    
        private static class Holder {
            private static final MyAppManager INSTANCE = new MyAppManager();
        }
    
        public static MyAppManager getInstance() {
            return Holder.INSTANCE;
        }
    
        public void addActivity(Activity activity) {
            mActivities.add(activity);
        }
    
        public void removeActivity(Activity activity) {
            hideSoftKeyBoard(activity);
            activity.finish();
            mActivities.remove(activity);
        }
    
        public void removeAllActivity() {
            for (Activity activity : mActivities) {
                hideSoftKeyBoard(activity);
                activity.finish();
            }
            mActivities.clear();
        }
    
        public <T extends Activity> boolean hasActivity(Class<T> tClass) {
            for (Activity activity : mActivities) {
                if (tClass.getName().equals(activity.getClass().getName())) {
                    return !activity.isDestroyed() || !activity.isFinishing();
                }
            }
            return false;
        }
    
        public void hideSoftKeyBoard(Activity activity) {
            View localView = activity.getCurrentFocus();
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (localView != null && imm != null) {
                imm.hideSoftInputFromWindow(localView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    
        /**
         * 退出应用
         */
        public void exit() {
            removeAllActivity();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    System.exit(0);
                }
            },500);
    
        }
    }
    
    
    

    相关文章

      网友评论

          本文标题:Android工作整理-Activity管理,包括退出登录

          本文链接:https://www.haomeiwen.com/subject/jstphktx.html