这是一个 Activity 管理类,可以有效的监控activity 的运作
/**
* @ 作者 CODE_LYF
* @ 时间 2018/1/13 0013.
* 代码人生
*/
public class MyApplication extends Application {
Map<String, String> globalMap;
/**
* 维护Activity 的list
*/
private static List<Activity> mActivitys = Collections.synchronizedList(new LinkedList<Activity>());
public Map<String, String> getGlobalMap() {
return globalMap;
}
@Override
public void onCreate() {
super.onCreate();
//初始化集合
globalMap = new HashMap<>();
registerActivityListener();
}
/**
* @param activity 作用说明 :添加一个activity到管理里
*/
public void pushActivity(Activity activity) {
mActivitys.add(activity);
MeLog.logTo("activityList:size:" + mActivitys.size());
}
/**
* @param activity 作用说明 :删除一个activity在管理里
*/
public void popActivity(Activity activity) {
mActivitys.remove(activity);
MeLog.logTo("activityList:size:" + mActivitys.size());
}
/**
* get current Activity 获取当前Activity(栈中最后一个压入的)
*/
public static Activity currentActivity() {
if (mActivitys == null || mActivitys.isEmpty()) {
return null;
}
Activity activity = mActivitys.get(mActivitys.size() - 1);
return activity;
}
/**
* 结束当前Activity(栈中最后一个压入的)
*/
public static void finishCurrentActivity() {
if (mActivitys == null || mActivitys.isEmpty()) {
return;
}
Activity activity = mActivitys.get(mActivitys.size() - 1);
finishActivity(activity);
}
/**
* 结束指定的Activity
*/
public static void finishActivity(Activity activity) {
if (mActivitys == null || mActivitys.isEmpty()) {
return;
}
if (activity != null) {
mActivitys.remove(activity);
activity.finish();
activity = null;
}
}
/**
* 结束指定类名的Activity
*/
public static void finishActivity(Class<?> cls) {
if (mActivitys == null || mActivitys.isEmpty()) {
return;
}
for (Activity activity : mActivitys) {
if (activity.getClass().equals(cls)) {
finishActivity(activity);
}
}
}
/**
* 按照指定类名找到activity
*
* @param cls
* @return
*/
public static Activity findActivity(Class<?> cls) {
Activity targetActivity = null;
if (mActivitys != null) {
for (Activity activity : mActivitys) {
if (activity.getClass().equals(cls)) {
targetActivity = activity;
break;
}
}
}
return targetActivity;
}
/**
* @return 作用说明 :获取当前最顶部activity的实例
*/
public Activity getTopActivity() {
Activity mBaseActivity = null;
synchronized (mActivitys) {
final int size = mActivitys.size() - 1;
if (size < 0) {
return null;
}
mBaseActivity = mActivitys.get(size);
}
return mBaseActivity;
}
/**
* 结束所有Activity
*/
public static void finishAllActivity() {
if (mActivitys == null) {
return;
}
for (Activity activity : mActivitys) {
activity.finish();
}
mActivitys.clear();
}
/**
* 退出应用程序
*/
public static void appExit() {
try {
MeLog.logTo("app exit");
finishAllActivity();
} catch (Exception e) {
}
}
/**
* 返回首页
*/
public static void returnHomeActivity() {
if (mActivitys == null || mActivitys.isEmpty()) {
return;
}
for (int i = mActivitys.size() - 1; i > 0; i--) {
finishActivity(mActivitys.get(i));
MeLog.logTo(i + "");
}
}
private void registerActivityListener() {
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
/**
* 监听到 Activity创建事件 将该 Activity 加入list
*/
pushActivity(activity);
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
if (null == mActivitys && mActivitys.isEmpty()) {
return;
}
if (mActivitys.contains(activity)) {
/**
* 监听到 Activity销毁事件 将该Activity 从list中移除
*/
popActivity(activity);
}
}
});
}
}