Android 开发过程中总是需要对项目中的 activity 进行管理。如果单独处理每个 activity 的生命周期会显得很多余,也很麻烦。所以这时候就需要一个 activity 管理工具来统一管理项目中的 activity 。
常见写法
public class ActivityManagerTool { private final List<Activity> activities = new LinkedList<Activity>(); private static ActivityManagerTool manager; private boolean isExist = false;// activity 存在标志 public static Class<?> indexActivity; // 首页所在的activity所对应的类名,必须在打开首页设置此项 public static List<Class<?>> bottomActivities = new LinkedList<Class<?>>();// 底部导航类集合 /** * 获得 activity管理对象 * * @return */ public static ActivityManagerTool getActivityManager() { if (null == manager) { manager = new ActivityManagerTool(); } return manager; } /** * 添加新的activity * * @param activity * @return */ public boolean add(final Activity activity) { int position = 0; // 判断是否自动清除非子activity// if (AbsInitApplication.isUseActivityManager) { // 导航栏activity进栈,删除非导航栏activity if (isBottomActivity(activity)) { for (int i = 0; i < activities.size() - 1; i++) { if (!isBottomActivity(activities.get(i))) { popActivity(activities.get(i)); i--; } if (i > 0) { // 获得重复activity位置 if (activities.get(i).getClass() .equals(activity.getClass())) { isExist = true; position = i; } } } }// } if (!activities.add(activity)) { return false; } // 删除重复activity if (isExist) { isExist = false; activities.remove(position); } return true; } /** * 关闭除参数activity外的所有activity * * @param activity */ public void finish(final Activity activity) { for (Activity iterable : activities) { if (activity != iterable) { iterable.finish(); } } } /** * 关闭所有的activity */ public void exit() { for (Activity activity : activities) { if (activity != null) { activity.finish(); } } try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.exit(0); } /** * 删除指定activity * * @param activity */ private void popActivity(final Activity activity) { if (activity != null) { activity.finish(); activities.remove(activity); } } /** * 删除已经finish的activity * * @param sourceActivity */ public void removeTemporaryActivities(final Class<Activity> targetclazz, final Activity sourceActivity) { if (targetclazz == null || sourceActivity == null) { return; } int begin = -1; int end = -1; Activity activity; for (int i = activities.size() - 1; i >= 0; i--) { activity = activities.get(i); if (activity.getClass() == targetclazz && end == -1) { end = i; } if (sourceActivity == activity && begin == -1) { begin = i; } if (begin != -1 && end != -1) { break; } } if (end != -1 && begin > end) { for (int i = begin; i > end; i--) { activity = activities.get(i); popActivity(activity); } } } /** * 获得当前activity * * @return */ @SuppressWarnings("unused") public Activity currentActivity() { if (activities == null || activities.isEmpty()) { return null; } Activity activity = activities.get(activities.size() - 1); return activity; } /* * 是否存在指定的Activity * */ public boolean isExistActivity(final Class<? extends Activity> targetclazz) { Activity activity; if (activities == null) return false; for (int i = activities.size() - 1; i >= 0; i--) { activity = activities.get(i); if (activity.getClass() == targetclazz) { return true; } } return false; } public boolean isTopActivity(final Activity activity) { Activity currentActivity = currentActivity(); if (currentActivity == null) return false; return currentActivity.equals(activity); } /** * activity是否为底部导航 * * @return */ public boolean isBottomActivity(final Activity activity) { for (int i = 0; i < bottomActivities.size(); i++) { if (activity.getClass() == bottomActivities.get(i)) { return true; } } return false; } /** * 如需返回IndexActivity则返回IndexActivity * * @param context */ public void backIndex(final Context context) { if (activities.size() <= 0) { return; } for (int i = activities.size() - 1; i >= 0; i--) { Activity activity = activities.get(i); if (isBottomActivity(activity)) { Intent intent = new Intent(); intent.setClass(context, indexActivity); context.startActivity(intent); } else { popActivity(activity); } } } /** * 如需返回IndexActivity则返回IndexActivity * * @param clazz */ public <E extends Activity> boolean backToActivity(final Class<E> clazz) { boolean flag = false; if (activities.size() <= 0) { return flag; } for (int i = activities.size() - 1; i >= 0; i--) { Activity activity = activities.get(i); if (activity.getClass() == clazz) { flag = true; break; } } if (flag) { for (int i = activities.size() - 1; i >= 0; i--) { Activity activity = activities.get(i); if (activity.getClass() != clazz) { popActivity(activity); } else { break; } } } return flag; } /** * 删除已经finish的activity * * @param activity */ public void removeActivity(final Activity activity) { if (activity != null) { activities.remove(activity); } } /** * 初始化,存储底部导航类 * * @param activityClass */ public void setBottomActivities(final Class<?> activityClass) { if (activityClass != null) { bottomActivities.add(activityClass); } } public List<Activity> getActivities() { return activities; } }
public class AppManager {
private static Stack<Activity> activityStack;
private static AppManager instance;
private AppManager(){
}
/**
* 单一实例
*/
public static AppManager getAppManager(){
if(instance==null){
instance=new AppManager();
}
return instance;
}
/**
* 添加Activity到堆栈
*/
public void addActivity(Activity activity){
if(activityStack==null){
activityStack=new Stack<Activity>();
}
activityStack.add(activity);
}
/**
* 获取当前Activity(堆栈中最后一个压入的)
*/
public Activity currentActivity(){
if(activityStack==null)
return null;
Activity activity=activityStack.lastElement();
return activity;
}
/**
* 结束当前Activity(堆栈中最后一个压入的)
*/
public void finishActivity(){
try{
if(activityStack==null)
return;
Activity activity=activityStack.lastElement();
finishActivity(activity);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 结束指定的Activity
*/
public void finishActivity(Activity activity){
if(activity!=null){
activityStack.remove(activity);
activity.finish();
activity=null;
}
}
/**
* 结束指定类名的Activity
*/
public void finishActivity(Class<?> cls){
for (Activity activity : activityStack) {
if(activity.getClass().equals(cls) ){
finishActivity(activity);
}
}
}
/**
* 结束指定的多个Activity
*/
public void finishActivityList(List<Class<?>> clsList){
for(int j = 0; j < clsList.size(); j++){
Class<?> cls = clsList.get(j);
for (int i = 0, size = activityStack.size(); i < size; i++){
if (activityStack.get(i).getClass().equals(cls)){
activityStack.get(i).finish();
}
}
}
}
/**
* 结束所有Activity
*/
public void finishAllActivity(){
for (int i = 0, size = activityStack.size(); i < size; i++){
if (null != activityStack.get(i)){
activityStack.get(i).finish();
}
}
activityStack.clear();
}
/**
* 退出应用程序
*/
public void AppExit(Context context) {
try {
finishAllActivity();
//退出程序
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
} catch (Exception e) {
}
}
}
在基类里调用,这样我们就不需要单独在某个Activity里面添加重复逻辑。
protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityManagerTool.getActivityManager().add(this);}
@Overrideprotected void onDestroy() { super.onDestroy(); DialogManager.hideDialog(); ActivityManagerTool.getActivityManager().removeActivity(this); EventBus.getDefault().unregister(BaseActivity.this);}