在APP開發(fā)中翩概,很多情況下,我們需要統(tǒng)一管理所有Activity酒甸。因為 不僅能實現(xiàn)在應用退出時關閉所有activity,還能實現(xiàn)獲取當前Activity上一個Activity、尋找某個類型的Activity、結束某個Activity拭荤,以及Activity之間的跳轉等操作。
以往的實現(xiàn)方式也很多疫诽,都比較麻煩舅世,但是幸運的是API 14之后旦委,在Application類中,提供了一個應用生命周期回調的注冊方法雏亚,用來對應用的生命周期進行集中管理缨硝,這個接口叫registerActivityLifecycleCallbacks,可以通過它注冊自己的ActivityLifeCycleCallback罢低,每一個Activity的生命周期都會回調到這里的對應方法查辩。
Activity生命周期圖:
ActivityLifecycleCallbacks接口代碼:
這種方式實現(xiàn)起來非常簡潔,通俗易懂网持,下面貼出我的實現(xiàn)
AppManageHelper
/**
* Activity統(tǒng)一管理的輔助類
* <p>
* 作者: 周旭 on 2017年9月15日 0015.
* 郵箱:374952705@qq.com
* 博客:http://www.reibang.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生命周期回調的統(tǒng)一管理
registerActivityLifecycleCallbacks(new SwitchBackgroundCallbacks());
}
//activity生命的回調
private class SwitchBackgroundCallbacks implements ActivityLifecycleCallbacks {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
/**
* 監(jiān)聽到 Activity創(chuàng)建事件 將該 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)){
/**
* 監(jiān)聽到 Activity銷毀事件 將該Activity 從list中移除
*/
AppManageHelper.popActivity(activity);
}
}
}
}