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);
}
}
网友评论