Activity管理工具类

app开发中,经常需要管理activity,google在 4.0 提供了一个 registerActivityLifecycleCallbacks 方法,可以监听所有activity的生命周期。因此,可以利用这个方法,管理所有的activity。上代码:

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import static android.os.Build.VERSION;
import static android.os.Build.VERSION_CODES;

/**
 * 描述: Activity 管理工具类( 需要android 4.0系统(14) )
 * 

* created: 2018/7/4/004 10:39 **/ public class ActivityManagerUtils { private static ActivityManagerUtils activityManagerUtils; private ActivityManagerUtils() { } public static ActivityManagerUtils getInstance() { if (activityManagerUtils == null) { synchronized (ActivityManagerUtils.class) { if (activityManagerUtils == null) { activityManagerUtils = new ActivityManagerUtils(); } } } return activityManagerUtils; } /** * 管理Activity的list集合 */ private static List mActivitys = Collections.synchronizedList(new LinkedList()); public List getmActivitys() { return mActivitys; } /** * 添加一个activity到list里 */ public void pushActivity(Activity activity) { if (mActivitys == null) { LogUtils.logMeActivity("pushActivity: mActivitys == null"); return; } mActivitys.add(activity); LogUtils.logMeActivity("pushActivity: activityList:size:" + mActivitys.size()); } /** * 从list里删除一个activity */ public void popActivity(Activity activity) { if (mActivitys == null) { LogUtils.logMeActivity("popActivity: mActivitys == null"); return; } mActivitys.remove(activity); LogUtils.logMeActivity("popActivity: activityList:size:" + mActivitys.size()); } /** * 结束指定类名的Activity */ public void finishActivity(Class cls) { if (mActivitys == null || mActivitys.isEmpty()) { return; } List activityList = new ArrayList<>(); for (Activity activity : mActivitys) { if (activity != null && activity.getClass().equals(cls)) { activityList.add(activity); } } mActivitys.removeAll(activityList); for (int i = 0; i < activityList.size(); i++) { Activity activity = activityList.get(i); if (activity != null) { activity.finish(); } } } /** * 结束指定类名的 Activity 集合 */ public void finishActivityCollection(@NonNull List> classList) { if (mActivitys == null || mActivitys.isEmpty()) { return; } List needFinishedActivityList = new ArrayList<>(); for (int i = 0; i < classList.size(); i++) { Class aClass = classList.get(i); for (Activity activity : mActivitys) { if (activity != null && activity.getClass().equals(aClass)) { if (!needFinishedActivityList.contains(activity)) { needFinishedActivityList.add(activity); } } } } mActivitys.removeAll(needFinishedActivityList); for (int j = 0; j < needFinishedActivityList.size(); j++) { Activity activity = needFinishedActivityList.get(j); if (activity != null) { activity.finish(); } } } /** * 结束所有Activity */ public void finishAllActivity() { if (mActivitys == null) { return; } List needFinishedActivityList = new ArrayList<>(); for (int i = 0; i < mActivitys.size(); i++) { for (Activity activity : mActivitys) { if (activity != null) { if (!needFinishedActivityList.contains(activity)) { needFinishedActivityList.add(activity); } } } } mActivitys.removeAll(needFinishedActivityList); LogUtils.logMeActivity("needFinishedActivityList :" + needFinishedActivityList); for (int j = 0; j < needFinishedActivityList.size(); j++) { Activity activity = needFinishedActivityList.get(j); if (activity != null) { activity.finish(); } } } public void registerActivityListener() { if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) { MyApp.INSTANCE.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { pushActivity(activity); } @Override public void onActivityStarted(Activity activity) { } @Override public void onActivityResumed(Activity activity) { LogUtils.logMeActivity("onActivityResumed: activityList:size:" + (mActivitys!=null?mActivitys.size():0)); } @Override public void onActivityPaused(Activity activity) { LogUtils.logMeActivity("onActivityPaused: activityList:size:" + (mActivitys!=null?mActivitys.size():0)); } @Override public void onActivityStopped(Activity activity) { LogUtils.logMeActivity("onActivityStopped: activityList:size:" + (mActivitys!=null?mActivitys.size():0)); } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { LogUtils.logMeActivity("onActivitySaveInstanceState: activityList:size:" + (mActivitys!=null?mActivitys.size():0)); } @Override public void onActivityDestroyed(Activity activity) { if (null == mActivitys || mActivitys.isEmpty()) { return; } if (mActivitys.contains(activity)) { popActivity(activity); } } }); } } }

 

 


代码中使用的 LogUtils 链接:https://blog.csdn.net/qq_34233110/article/details/82902516

 

使用:

//在 Application里注册
//管理activity
ActivityManagerUtils.getInstance().registerActivityListener();


//在需要关闭所有acivity的地方调用
ActivityManagerUtils.getInstance().finishAllActivity();

 

参考文档:

  1. 优雅的使用ActivityLifecycleCallbacks管理Activity和区分App前后台 https://blog.csdn.net/u010072711/article/details/77090313
  2. Android Activity 全局管理 终极解决方案   https://blog.csdn.net/blogblj/article/details/52068457

 

 

 

 

你可能感兴趣的:(工具类,android开发)