Android-- 关闭某个指定 activity,android 游戏开发实战
####优劣:优点:最常规使用,不会出现内存泄漏,在基类中操作,代码量不多。缺点:项目中若是需要关闭页面多的话,需要发送大量广播,会降低性能。##方案二 ####直接 static activity 方式这是网上提供的,代码一看就明显的内存泄漏。但是。。。。。。
private static AActivity sInstance;public static AActivity getInstance() {if (sInstance != null) {return sInstance;}return null;}public static void finishActivity() {if (sInstance != null) {sInstance.finish();}}
内存泄漏明显,之所以有但是,那是因为这种方式让我想到加入弱引用 WeakReference 的方式。
public class BActivity extends BaseActivity {private static WeakReference<BActivity> sActivityRef;public static void finishActivity() {if (sActivityRef != null && sActivityRef.get() != null) {sActivityRef.get().finish();}}@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_b);sActivityRef = new WeakReference<>(this);Button btnB = (Button) findViewById(R.id.btn_b);btnB.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(BActivity.this, CActivity.class));}});}}
WeakReference 以及 JVM 相关,虽然 WeakReference 被 static 了,生命周期变长了,但是 WeakReference 关联的对象不受影响的,依然遵循 WeakReference 在 gc 时候的规则。优劣: 优点:代码简单直观,引入 WeakReference,解决了内存泄漏问题。 缺点:在每个 activity 中都要添加,代码量多。(不知是否可抽取到基类中,以后尝试)##方案三 ####使用集合或者栈的方式
public class FinishActivityManager {private FinishActivityManager() {}private static FinishActivityManager sManager;private List<Activity> activityList;public static FinishActivityManager getManager() {if (sManager == null) {synchronized (FinishActivityManager.class) {if (sManager == null) {sManager = new FinishActivityManager();}}}return sManager;}/**
添加 Activity 到集合中/public void addActivity(Activity activity) {if (activityList == null) {activityList = new LinkedList<>();}activityList.add(activity);}/*
关闭指定的 Activity/public void finishActivity(Activity activity) {if (activityList != null && activity != null && activityList.contains(activity)) {//使用迭代器安全删除 for (Iterator<Activity> it = activityList.iterator(); it.hasNext(); ) {Activity temp = it.next();// 清理掉已经释放的 activityif (temp == null) {it.remove();continue;}if (temp == activity) {it.remove();}}activity.finish();}}/*
关闭指定类名的 Activity/public void finishActivity(Class<?> cls) {if (activityList != null) {// 使用迭代器安全删除 for (Iterator<Activity> it = activityList.iterator(); it.hasNext(); ) {Activity activity = it.next();// 清理掉已经释放的 activityif (activity == null) {it.remove();continue;}if (activity.getClass().equals(cls)) {it.remove();activity.finish();}}}}/*
关闭所有的 Activity/public void finishAllActivity() {if (activityList != null) {for (Iterator<Activity> it = activityList.iterator(); it.hasNext(); ) {Activity activity = it.next();if (activity != null) {activity.finish();}}activityList.clear();}}/*
退出应用程序*/public void exitApp() {try {finishAllActivity();// 退出 JVM,释放所占内存资源,0 表示正常退出 System.exit(0);// 从系统中 kill 掉应用程序 android.os.Process.killProcess(android.os.Process.myPid());} catch (Exception e) {e.printStackTrace();}}}
这种方式注意在删除的时候使用迭代器,否则会出现并发修改异常。 那就来说说但是,应用中要是不使用这种封装的方式去 finish 掉 Activity 的时候,封装的 manager 并不知道,没有断开并释放掉其引用,会引起内存泄漏。所以 WeakReference 又要出场了。
public class FinishActivityManager {private FinishActivityManager() {}private static FinishActivityManager sManager;private Stack<WeakReference<Activity>> mActivityStack;public static FinishActivityManager getManager() {if (sManager == null) {synchronized (FinishActivityManager.class) {if (sManager == null) {sManager = new FinishActivityManager();}}}return sManager;}/**
添加 Activity 到栈
@param activity/public void addActivity(Activity activity) {if (mActivityStack == null) {mActivityStack = new Stack<>();}mActivityStack.add(new WeakReference<>(activity));}/*
检查弱引用是否释放,若释放,则从栈中清理掉该元素/public void checkWeakReference() {if (mActivityStack != null) {// 使用迭代器进行安全删除 for (Iterator<WeakReference<Activity>> it = mActivityStack.iterator(); it.hasNext(); ) {WeakReference<Activity> activityReference = it.next();Activity temp = activityReference.get();if (temp == null) {it.remove();}}}}/*
获取当前 Activity(栈中最后一个压入的)
@return/public Activity currentActivity() {checkWeakReference();if (mActivityStack != null && !mActivityStack.isEmpty()) {return mActivityStack.lastElement().get();}return null;}/*
关闭当前 Activity(栈中最后一个压入的)/public void finishActivity() {Activity activity = currentActivity();if (activity != null) {finishActivity(activity);}}/*
关闭指定的 Activity
@param activity*/public void finishActivity(Activity activity) {if (activity !
= null && mActivityStack != null) {// 使用迭代器进行安全删除 for (Iterator<WeakReference<Activity>> it = mActivityStack.iterator(); it.hasNext(); ) {WeakReference<Activity> activityReference = it.next();Activity temp = activityReference.get();// 清理掉已经释放的 activityif (temp == null) {it.remove();continue;}if (temp == activity) {it.remove();
评论