覆盖Android后退按钮

发布于 2024-12-06 11:16:42 字数 181 浏览 0 评论 0原文

关于我为什么尝试这样做的一些信息:我正在使用 ActivityGroups 从 tabHost 活动打开一个活动,并将该新活动保留在选项卡下。那部分我已经有了。但是,在该新活动中,如果我使用后退按钮,它会将我直接退出选项卡活动,因此我必须单击几次才能返回到原来的位置。

有没有办法将后退按钮设置为转到特定活动而不是杀死当前活动窗口?

A little info as to why I am attempting to do this: I am using ActivityGroups to open an activity from a tabHost activity and have that new activity stay under the tabs. That part i've got. But when in that new activity, if I use the back button it takes me right out of the tabs activity so I have to click a few times to get back to where I was.

Is there a way to set the back button to go to a specific activity rather than killing the current activity window?

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

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

发布评论

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

评论(2

神经暖 2024-12-13 11:16:42

我相信您应该能够执行以下操作:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        // start new Activity here
    }
    return super.onKeyDown(keyCode, event);
}

但是不建议覆盖后退按钮的预期功能。

I believe you should be able to do something like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        // start new Activity here
    }
    return super.onKeyDown(keyCode, event);
}

But overriding the expected functionality of the back button is not advisable.

且行且努力 2024-12-13 11:16:42

一般来说,我建议不要这样做,因为它破坏了用户体验。用户希望后退按钮能够杀死整个窗口,特别是因为您正在使用 tabhost。对于用户来说,整个一堆(选项卡和所有)都是一个他想在点击后退按钮时退出的活动。

如果您仍想这样做,请参阅 #onBackPressed ()。当活动检测到用户按下后退键时调用它。默认情况下是完成活动,但您可以让它执行您想要的任何操作。我建议小心谨慎。

您可能会从此处找到一些灵感。

In general, I would advise against that because it breaks the UX. The user expects the back button to kill the entire window, especially since you are using the tabhost. To the user, the entire bunch (tabs and all) is a single activity that he wants to exit when he hits the back button.

If you still want to do it, refer to #onBackPressed(). It is called when the activity has detected the user's press of the back key. The default is to finish the activity, but you can make it do whatever you want. I advise care and caution.

You might find some inspiration from here.

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