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


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 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  boolean hasActivity(Class 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管理,包括退出登录)