Android--关闭某个指定activity,android开发游戏

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 it = activityList.iterator(); it.hasNext(); ) {
    Activity temp = it.next();
    // 清理掉已经释放的activity
    if (temp == null) {
    it.remove();
    continue;
    }
    if (temp == activity) {
    it.remove();
    }
    }
    activity.finish();
    }
    }
    /
    *
  • 关闭指定类名的Activity
    /
    public void finishActivity(Class cls) {
    if (activityList != null) {
    // 使用迭代器安全删除
    for (Iterator it = activityList.iterator(); it.hasNext(); ) {
    Activity activity = it.next();
    // 清理掉已经释放的activity
    if (activity == null) {
    it.remove();
    continue;
    }
    if (activity.getClass().equals(cls)) {
    it.remove();
    activity.finish();
    }
    }
    }
    }
    /
    *
  • 关闭所有的Activity
    /
    public void finishAllActivity() {
    if (activityList != null) {
    for (Iterator 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 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 it = mActivityStack.iterator(); it.hasNext(); ) {
    WeakReference 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 it = mActivityStack.iterator(); it.hasNext(); ) {
    WeakReference activityReference = it.next();
    Activity temp = activityReference.get();
    // 清理掉已经释放的activity
    if (temp == null) {
    it.remove();
    continue;
    }
    if (temp == activity) {
    it.remove();
    }
    }
    activity.finish();
    }
    }
    /
    *
  • 关闭指定类名的所有Activity
  • @param cls
    */
    public void finishActivity(Class cls) {
    if (mActivityStack != null) {
    // 使用迭代器进行安全删除
    for (Iterator it = mActivityStack.iterator(); it.hasNext(); ) {
    WeakReference activityReference = it.next();
    Activity activity = activityReference.get();
    // 清理掉已经释放的activity
    if (activity == null) {
    it.remove();
    continue;
    }

本文在开源项目:Android开发不会这些?如何面试拿高薪! 中已收录,里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

C%80%E5%8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md) 中已收录,里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

Android--关闭某个指定activity,android开发游戏_第1张图片

你可能感兴趣的:(程序员,面试,移动开发,android)