关闭任务中的所有活动 [英] Closing all activities in a task

查看:118
本文介绍了关闭任务中的所有活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是不对的code,当我想关闭任务的所有活动?

 公共静态无效closeAllBelowActivities(活动电流)
{
    布尔标志= TRUE;
    下面= current.getParent活性();
    如果(以下== NULL)
        返回;
    的System.out.println(下面父:+ below.getClass());
    而(标志){
        活动TEMP =以下;
        尝试 {
            下面= temp.getParent();
            temp.finish();
        }赶上(例外五){
            标志= FALSE;
        }
    }
}
 

解决方案

该方法的android工程是活动的生活在一个堆栈(你可能知道),所以如果A调用B调用的C调用ð

该协议栈看起来像

  //ð - 电流
// C
// B
// 一个
 

如果你正在尝试做的是确保C,B,A是不是在堆栈中了,那么你应该打电话叫下一个活动前完成()

  A.startActivity(B)
A.finsh()
  ---
  B.startActivity(C)
  B.finish()
 

等等,所以我想我的下一个问题是,为什么你想做到这一点,通过从调用活动这是它被设计的方式,当前的活动对?

您还可以使用意向的标志像FLAG_ACTIVITY_CLEAR_TOP清除所有但最新的活动的历史记录列表,这可能是你正在做什么?

What is wrong with this code when I want to close all activities in a task?

public static void closeAllBelowActivities(Activity current)
{
    boolean flag = true;
    Activity below = current.getParent();
    if (below == null)
        return;
    System.out.println("Below Parent: " + below.getClass());
    while (flag) {
        Activity temp = below;
        try {
            below = temp.getParent();
            temp.finish();
        } catch (Exception e) {
            flag = false;
        }
    }
}

解决方案

The way android works is that activities live in a stack (which you may know) so if A calls B calls C calls D

The stack looks like

// D - Current 
// C
// B
// A

If all you are trying to do is make sure that C,B,A are not in the stack anymore then you should call finish() before calling the next activity

A.startActivity(B)
A.finsh()
  ---
  B.startActivity(C)
  B.finish()

And so on, so I guess the my next question is why are you trying to do that via the current activity vs. from the calling activity which is the way it was designed?

You can also use intent flags like FLAG_ACTIVITY_CLEAR_TOP to clear the history list of all but the newest activity, which maybe is what you are trying to do?

这篇关于关闭任务中的所有活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆