在APP开发中,很多情况下,我们需要统一管理所有Activity。因为 不仅能实现在应用退出时关闭所有activity,还能实现获取当前Activity上一个Activity、寻找某个类型的Activity、结束某个Activity,以及Activity之间的跳转等操作。
以往的实现方式也很多,都比较麻烦,但是幸运的是API 14之后,在Application类中,提供了一个应用生命周期回调的注册方法,用来对应用的生命周期进行集中管理,这个接口叫registerActivityLifecycleCallbacks,可以通过它注册自己的ActivityLifeCycleCallback,每一个Activity的生命周期都会回调到这里的对应方法。
Activity生命周期图:
ActivityLifecycleCallbacks接口代码:
这种方式实现起来非常简洁,通俗易懂,下面贴出我的实现
AppManageHelper
/**
* Activity统一管理的辅助类
* <p>
* 作者: 周旭 on 2017年9月15日 0015.
* 邮箱:374952705@qq.com
* 博客:http://www.jianshu.com/u/56db5d78044d
*/
public class AppManageHelper {
private static final String TAG = "AppManageHelper";
/**
* 维护Activity 的list
*/
public static List<Activity> mActivityList = Collections
.synchronizedList(new LinkedList<Activity>());
/**
* @param activity 作用说明 :添加一个activity到管理里
*/
public static void pushActivity(Activity activity) {
mActivityList.add(activity);
Log.i(TAG, "activityList:size:" + mActivityList.size());
}
/**
* @param activity 作用说明 :删除一个activity在管理里
*/
public static void popActivity(Activity activity) {
mActivityList.remove(activity);
Log.i(TAG, "activityList:size:" + mActivityList.size());
}
/**
* get current Activity 获取当前Activity(栈中最后一个压入的)
*/
public static Activity currentActivity() {
if (mActivityList == null || mActivityList.isEmpty()) {
return null;
}
Activity activity = mActivityList.get(mActivityList.size() - 1);
return activity;
}
/**
* 结束当前Activity(栈中最后一个压入的)
*/
public static void finishCurrentActivity() {
if (mActivityList == null || mActivityList.isEmpty()) {
return;
}
Activity activity = mActivityList.get(mActivityList.size() - 1);
finishActivity(activity);
}
/**
* 结束指定的Activity
*/
public static void finishActivity(Activity activity) {
if (mActivityList == null || mActivityList.isEmpty()) {
return;
}
if (activity != null) {
mActivityList.remove(activity);
activity.finish();
activity = null;
}
}
/**
* 结束指定类名的Activity
*/
public static void finishActivity(Class<?> cls) {
if (mActivityList == null || mActivityList.isEmpty()) {
return;
}
for (Activity activity : mActivityList) {
if (activity.getClass().equals(cls)) {
finishActivity(activity);
}
}
}
/**
* 按照指定类名找到activity
*
* @param cls
* @return
*/
public static Activity findActivity(Class<?> cls) {
Activity targetActivity = null;
if (mActivityList != null) {
for (Activity activity : mActivityList) {
if (activity.getClass().equals(cls)) {
targetActivity = activity;
break;
}
}
}
return targetActivity;
}
/**
* @return 作用说明 :获取当前最顶部activity的实例
*/
public Activity getTopActivity() {
Activity mBaseActivity = null;
synchronized (mActivityList) {
final int size = mActivityList.size() - 1;
if (size < 0) {
return null;
}
mBaseActivity = mActivityList.get(size);
}
return mBaseActivity;
}
/**
* @return 作用说明 :获取当前最顶部的acitivity 名字
*/
public String getTopActivityName() {
Activity mBaseActivity = null;
synchronized (mActivityList) {
final int size = mActivityList.size() - 1;
if (size < 0) {
return null;
}
mBaseActivity = mActivityList.get(size);
}
return mBaseActivity.getClass().getName();
}
/**
* 结束所有Activity
*/
public static void finishAllActivity() {
if (mActivityList == null) {
return;
}
for (Activity activity : mActivityList) {
activity.finish();
}
mActivityList.clear();
}
/**
* 退出应用程序
*/
public static void exitApp() {
try {
Log.i(TAG, "app exit");
finishAllActivity();
} catch (Exception e) {
}
}
}
Application
public class MyApplication extends MultiDexApplication {
public static Context mContext;
@Override
public void onCreate() {
super.onCreate();
//注册Activity生命周期回调的统一管理
registerActivityLifecycleCallbacks(new SwitchBackgroundCallbacks());
}
//activity生命的回调
private class SwitchBackgroundCallbacks implements ActivityLifecycleCallbacks {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
/**
* 监听到 Activity创建事件 将该 Activity 加入list
*/
AppManageHelper.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 bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
if (null== AppManageHelper.mActivityList||AppManageHelper.mActivityList.isEmpty()){
return;
}
if (AppManageHelper.mActivityList.contains(activity)){
/**
* 监听到 Activity销毁事件 将该Activity 从list中移除
*/
AppManageHelper.popActivity(activity);
}
}
}
}