使用 Fragments 为 Android 中的每个选项卡单独返回堆栈 [英] Separate Back Stack for each tab in Android using Fragments

查看:25
本文介绍了使用 Fragments 为 Android 中的每个选项卡单独返回堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Android 应用中实现导航标签.由于 TabActivity 和 ActivityGroup 已弃用,我想改用 Fragments 来实现它.

I'm trying to implement tabs for navigation in an Android app. Since TabActivity and ActivityGroup are deprecated I would like to implement it using Fragments instead.

我知道如何为每个选项卡设置一个片段,然后在单击选项卡时切换片段.但是如何为每个选项卡设置一个单独的后堆栈?

I know how to set up one fragment for each tab and then switch fragments when a tab is clicked. But how can I have a separate back stack for each tab?

例如,片段 A 和 B 将在选项卡 1 下,片段 C 和 D 将在选项卡 2 下.当应用程序启动时,将显示片段 A 并选择选项卡 1.Then Fragment A might be replaced with Fragment B. When Tab 2 is selected Fragment C should be displayed.如果选择了选项卡 1,则应再次显示片段 B.此时应该可以使用后退按钮显示Fragment A.

For an example Fragment A and B would be under Tab 1 and Fragment C and D under Tab 2. When the app is started Fragment A is shown and Tab 1 is selected. Then Fragment A might be replaced with Fragment B. When Tab 2 is selected Fragment C should be displayed. If Tab 1 is then selected Fragment B should once again be displayed. At this point it should be possible to use the back button to show Fragment A.

此外,在设备旋转时保持每个选项卡的状态也很重要.

Also, it is important that the state for each tab is maintained when the device is rotated.

BR马丁

推荐答案

该框架目前不会自动为您执行此操作.您需要为每个选项卡构建和管理自己的后台堆栈.

The framework won't currently do this for you automatically. You will need to build and manage your own back stacks for each tab.

老实说,这似乎是一件非常值得怀疑的事情.我无法想象它会产生一个像样的用户界面——如果后退键会根据我所在的选项卡做不同的事情,特别是如果后退键在顶部时也有关闭整个活动的正常行为堆栈……听起来很糟糕.

To be honest, this seems like a really questionable thing to do. I can't imagine it resulting in a decent UI -- if the back key is going to do different things depending on the tab I am, especially if the back key also has its normal behavior of closing the entire activity when at the top of the stack... sounds nasty.

如果您正在尝试构建类似 Web 浏览器 UI 的东西,要获得对用户来说自然的 UX 将涉及根据上下文对行为进行许多微妙的调整,因此您肯定需要做自己的后台堆栈管理,而不是依赖于框架中的一些默认实现.例如,尝试注意后退键如何以您可以进入和退出的各种方式与标准浏览器交互.(浏览器中的每个窗口"本质上都是一个选项卡.)

If you are trying to build something like a web browser UI, to get a UX that is natural to the user is going to involve a lot of subtle tweaks of behavior depending on context, so you'll definitely need to do your own back stack management rather than rely on some default implementation in the framework. For an example try paying attention to how the back key interacts with the standard browser in the various ways you can go in and out of it. (Each "window" in the browser is essentially a tab.)

这篇关于使用 Fragments 为 Android 中的每个选项卡单独返回堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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