覆盖Android后退按钮
关于我为什么尝试这样做的一些信息:我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您应该能够执行以下操作:
但是不建议覆盖后退按钮的预期功能。
I believe you should be able to do something like this:
But overriding the expected functionality of the back button is not advisable.
一般来说,我建议不要这样做,因为它破坏了用户体验。用户希望后退按钮能够杀死整个窗口,特别是因为您正在使用 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.