關於
好久沒有更新文章了,感覺該寫點什麼了。就簡單點的啦,這次分享一個Activity管理工具,輕鬆關閉想關閉的頁面。寫這個的原因也是因為,在當前頁面需要關閉上一個頁面或其他之前開啟的頁面。其實網上也有很多的辦法,比如static修飾,比如Application、佇列管理等等,感比較麻煩,不注意容易記憶體洩漏。所以自己搞了一個管理類,只需要在Application中的onCreate方法中初始化一次,就OK了,剩下的就是你要關哪個頁面了。
演示
還是先來個GIF圖演示一下吧
操作
- 初始化
自定義Application,在onCreate中新增XActivityManager.addActivityListener(this);
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
XActivityManager.addActivityListener(this);
}
}
複製程式碼
- 怎麼用?
假如說當前有Activity:
MainActivity、OneActivity、TwoActivity、ThreeActivity、FourActivity、FiveActivity
- 當你在
ThreeActivity
關閉TwoActivity
:
XActivityManager.getInstance().finishActivityByName(TwoActivity.class);
複製程式碼
- 當你在
FourActivity
想關閉所有Activity排除MainActivity
XActivityManager.getInstance().finishAllExceptThisActivity(MainActivity.class);
複製程式碼
實現思路
程式碼在下面大家可以先看一看
- 程式碼的主要思路在於新增了關於Application對Activity的監聽,當監聽到有Activity執行是則新增到Map中,如果Activity銷燬則從Map中刪除。
- 用Map管理,是方便與通過名字就可以finish掉Activity。
- 由於可能造成記憶體洩漏,Map新增的是含有Activity的WeakReference。
- 通過方法傳入Activity的class,然後獲取Activity名字,通過Map的key得到Activity。
程式碼
import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
public class XActivityManager {
private static XActivityManager sInstance = new XActivityManager();
private WeakReference<Activity> sCurrentActivityWeakRef;
private Map<String, WeakReference<Activity>> mAllActivity;
private XActivityManager() {
mAllActivity = new HashMap<>();
}
public static XActivityManager getInstance() {
return sInstance;
}
/**
* 獲取當前Activity
* @return
*/
public Activity getCurrentActivity() {
Activity currentActivity = null;
if (sCurrentActivityWeakRef != null) {
currentActivity = sCurrentActivityWeakRef.get();
}
return currentActivity;
}
private void setCurrentActivity(Activity activity) {
sCurrentActivityWeakRef = new WeakReference<>(activity);
mAllActivity.put(activity.getClass().getSimpleName(), sCurrentActivityWeakRef);
for (Map.Entry<String, WeakReference<Activity>> entry :
mAllActivity.entrySet()) {
WeakReference<Activity> wr = entry.getValue();
if (wr.get() == null) continue;
Log.i("XActivityManager", "當前活動Activity:" + wr.get().getClass().getSimpleName());
}
Log.i("XActivityManager", "---------------------------------------");
}
private void removeThisActivity(Activity activity) {
mAllActivity.remove(activity.getClass().getSimpleName());
}
/**
* 通過Activity 名字幹掉這個Activity
*
* @param activityClass
*/
public void finishActivityByName(Class<? extends Activity> activityClass) {
WeakReference<Activity> wr = mAllActivity.get(activityClass.getSimpleName());
if (wr == null) return;
Activity activity = wr.get();
if (
activity == null
|| activity.isFinishing()
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed())
)
return;
activity.finish();
}
/**
* 幹掉所有的activity排除這個Activity
*/
public void finishAllExceptThisActivity(Class<? extends Activity> activityClass) {
for (Map.Entry<String, WeakReference<Activity>> entry :
mAllActivity.entrySet()) {
WeakReference<Activity> wr = entry.getValue();
if (
wr.get() == null
|| wr.get().isFinishing()
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && wr.get().isDestroyed())
|| entry.getKey().equals(activityClass.getSimpleName())
)
continue;
wr.get().finish();
}
}
/**
* 新增對Activity的監聽
*/
public static void addActivityListener(Application app) {
app.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
XActivityManager.getInstance().setCurrentActivity(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) {
XActivityManager.getInstance().removeThisActivity(activity);
}
});
}
}
複製程式碼
END