ActivityGroup 不处理 ListActivity 的后退键

发布于 2024-09-25 13:51:38 字数 538 浏览 5 评论 0原文

我正在使用 ActivityGroup 生成多个活动,并在 TabActivity 的同一选项卡内切换视图。

当我按下后退键时,会在 ActivityGroup 内调用此方法,

public void back() {  
        if(history.size() > 0) {  
            history.remove(history.size()-1);
            if (history.size() > 0)
             setContentView(history.get(history.size()-1)); 
            else
              initView();
        }else {  
            finish();  
        }  
    }  

此方法允许我保留一堆活动,并在按下后退键时返回到上一个活动。

这在我的所有嵌套活动中都运行良好,但在 ListActivity 上按后退键将简单地退出应用程序。

I am using an ActivityGroup to spawn multiple activities and switch views from within the same tab in a TabActivity.

When I press the back key this method is called inside my ActivityGroup

public void back() {  
        if(history.size() > 0) {  
            history.remove(history.size()-1);
            if (history.size() > 0)
             setContentView(history.get(history.size()-1)); 
            else
              initView();
        }else {  
            finish();  
        }  
    }  

this method allows me to keep a stack of my activities and go back to the previous one when the back key is pressed.

this is working well on all my nested activities, except on a ListActivity where a press on the back key will simply exit the application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

动次打次papapa 2024-10-02 13:51:38

在 ActivityGroup 中,当 ListActivity 处于焦点时 ActivityGroup 的 onKeyDown() 不会被调用,只有子级(ListActivity)的 onKeyDown() 被调用
为了确保 ActivityGroup 的 onKeyDown() 被调用,我们需要从 ListActivity 的 onKeyDown() 返回 false。完成此更改后,我能够接收关键事件

In a ActivityGroup, When ListActivity is in focus onKeyDown() of ActivityGroup is not getting called, only child's (ListActivity) onKeyDown() is getting called
To make sure ActivityGroup's onKeyDown() is called, We need to return false from onKeyDown() of ListActivity. After doing this change I am able to receive key events

网名女生简单气质 2024-10-02 13:51:38

我知道你的意思...几周前我遇到了这个问题。我也知道这是一个烦人的错误,并且我已经吸取了教训:我永远不会使用这种方法!因此,基本上,为了解决这个问题,您必须对代码进行一些解决方法。例如,我通过将以下代码添加到活动之一来解决该问题:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && StatsGroupActivity.self != null) {
        StatsGroupActivity.self.popView();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

请注意,我的 ActivityGroup 称为 StatsGroupActivity ,如下所示:

public class StatsGroupActivity extends GroupActivity{

    /**
     * Self reference to this group activity
     */
    public static StatsGroupActivity self;

    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        self = this;
        // more stuff
    }
}

I know what you mean... I faced that problem some weeks ago. I also know it's an annoying bug and I've learned the lesson: I won't use that approach ever! So, basically, in order to fix this you will have to do some workarounds to your code. For instance, I fixed that problem with one of my activities adding this code to the activity:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && StatsGroupActivity.self != null) {
        StatsGroupActivity.self.popView();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Notice that my ActivityGroup is called StatsGroupActivity and looks like:

public class StatsGroupActivity extends GroupActivity{

    /**
     * Self reference to this group activity
     */
    public static StatsGroupActivity self;

    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        self = this;
        // more stuff
    }
}
不必你懂 2024-10-02 13:51:38

@Cristian

我使用的是普通的Activity而不是ListActivity,但是填充了ListView后它给了我同样的问题。

我仅在 Activity 上实现 onBackPressed 而不是 onKeyDown 来回调 MyActivityGroup 调用的相同 back() 函数。

@Override  
public void onBackPressed() {
    MyActivityGroup.group.back();  
    return;  
}

group 是 MyActivityGroup 中的静态字段。

public static MyActivityGroup group; 

back() 函数与 yann.debonnel 提供的相同。

我不知道你的 ListActivity 是否是同样的情况,没有测试。但就我而言,它有效。

@Cristian

I am using a normal Activity instead of ListActivity, but with a ListView populated it gave me the same problem.

I only implemented onBackPressed on my Activity instead of onKeyDown to call back the same back() function that MyActivityGroup invoked.

@Override  
public void onBackPressed() {
    MyActivityGroup.group.back();  
    return;  
}

group is a static field in MyActivityGroup.

public static MyActivityGroup group; 

The back() function would be the same as yann.debonnel provided.

I don't know if this is the same case for your ListActivity, didn't test it. But in my case it worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文