Android Activity 全局管理 終極解決方案
管理Activity的好處
在Android 開發(fā)中 Activity 的管理 對于一個App是至關(guān)重要的,管理所有Activity 不僅能實(shí)現(xiàn)在應(yīng)用推出時關(guān)閉所有activity,還能實(shí)現(xiàn)獲取當(dāng)前Activity上一個Activity、尋找某個類型的Activity悉尾、結(jié)束某個Activity,以及Activity之間的跳轉(zhuǎn)等多種交互操作。
傳統(tǒng)的實(shí)現(xiàn)方式
之前管理Activity 的方式是在BaseActivity onCreate 方法中將Activity 實(shí)例存入工具類 或者BaseActivity 中維護(hù)的一個List或stack中,這樣能應(yīng)對多數(shù)情況下的問題腮介,但假如有Activity沒有繼承封裝的BaseActivity,那每個Activity 就都要在onCreate方法中寫添加到維護(hù)list的代碼端衰,同時當(dāng)Activity銷毀時萤厅,也要從list中將Activity移除橄抹,等等,邏輯可謂復(fù)雜惕味,代碼也較難維護(hù)楼誓。
終極解決方案
所幸的是,自Android 4.0(API-14)開始名挥,Application 中多了一個方法疟羹,可以設(shè)置全局監(jiān)聽Activity的生命周期。這個方法就是:registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks activityLifecycleCallbacks)在傳入的參數(shù) activityLifecycleCallbacks 能得到全局所有Activity 生命周期的回調(diào)禀倔,因此 我們可以從Application中全局監(jiān)聽所有Activity 并對其進(jìn)行管理榄融,再也不用在Activity中處理那些扯淡的邏輯,不廢話救湖,直接上代碼:
package com.kevin.baselibrary.app;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import com.kevin.baselibrary.utils.LogUtils;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* Created by bailiangjin on 16/4/11.
*/
public abstract class AbsSuperApplication extends Application {
protected static Context context;
protected static String appName;
/**
* 維護(hù)Activity 的list
*/
private static List<Activity> mActivitys = Collections
.synchronizedList(new LinkedList<Activity>());
@Override
public void onCreate() {
super.onCreate();
context = this.getApplicationContext();
appName = getAppNameFromSub();
registerActivityListener();
}
public static String getAppName() {
return appName;
}
public static Context getContext() {
return context;
}
protected abstract String getAppNameFromSub();
/**
* @param activity 作用說明 :添加一個activity到管理里
*/
public void pushActivity(Activity activity) {
mActivitys.add(activity);
LogUtils.d("activityList:size:"+mActivitys.size());
}
/**
* @param activity 作用說明 :刪除一個activity在管理里
*/
public void popActivity(Activity activity) {
mActivitys.remove(activity);
LogUtils.d("activityList:size:"+mActivitys.size());
}
/**
* get current Activity 獲取當(dāng)前Activity(棧中最后一個壓入的)
*/
public static Activity currentActivity() {
if (mActivitys == null||mActivitys.isEmpty()) {
return null;
}
Activity activity = mActivitys.get(mActivitys.size()-1);
return activity;
}
/**
* 結(jié)束當(dāng)前Activity(棧中最后一個壓入的)
*/
public static void finishCurrentActivity() {
if (mActivitys == null||mActivitys.isEmpty()) {
return;
}
Activity activity = mActivitys.get(mActivitys.size()-1);
finishActivity(activity);
}
/**
* 結(jié)束指定的Activity
*/
public static void finishActivity(Activity activity) {
if (mActivitys == null||mActivitys.isEmpty()) {
return;
}
if (activity != null) {
mActivitys.remove(activity);
activity.finish();
activity = null;
}
}
/**
* 結(jié)束指定類名的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 作用說明 :獲取當(dāng)前最頂部activity的實(shí)例
*/
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;
}
/**
* @return 作用說明 :獲取當(dāng)前最頂部的acitivity 名字
*/
public String getTopActivityName() {
Activity mBaseActivity = null;
synchronized (mActivitys) {
final int size = mActivitys.size() - 1;
if (size < 0) {
return null;
}
mBaseActivity = mActivitys.get(size);
}
return mBaseActivity.getClass().getName();
}
/**
* 結(jié)束所有Activity
*/
public static void finishAllActivity() {
if (mActivitys == null) {
return;
}
for (Activity activity : mActivitys) {
activity.finish();
}
mActivitys.clear();
}
/**
* 退出應(yīng)用程序
*/
public static void appExit() {
try {
LogUtils.e("app exit");
finishAllActivity();
} catch (Exception e) {
}
}
private void registerActivityListener() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
/**
* 監(jiān)聽到 Activity創(chuàng)建事件 將該 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)){
/**
* 監(jiān)聽到 Activity銷毀事件 將該Activity 從list中移除
*/
popActivity(activity);
}
}
});
}
}
}
下面是運(yùn)行App時 全局監(jiān)聽 添加或者移除Activity的回調(diào)時打下的維護(hù)的list中Activity數(shù)量的變動日志:
工程源碼:
點(diǎn)擊此處 查看該示例Github工程源碼
其他文章推薦:
Android網(wǎng)絡(luò)請求發(fā)展簡史和RxJava+Retrofit+OkHttp實(shí)踐
Android BaseAdapter的極簡封裝