问题描述
为什么 FragmentTwo
从backstack在下列情况下消失:
Why does FragmentTwo
disappear from the backstack in the following situation:
- 在我的应用程序有一个
片段
名为FragmentOne
在活动
。 -
FragmentOne
持有按钮
。单击时,它会启动FragmentTwo
,它被添加到片段
backstack。 -
FragmentTwo
有一个按钮
,点击它时,增加了两个选项卡动作条
链接到两个片段
,FragmentThree
和FragmentFour
。FragmentThree
是可见的。 - 如果我现在单击后退按钮,我期望看到
FragmentTwo
。相反,我看到FragmentOne
。哪里FragmentTwo
去了?
- My app has a
Fragment
calledFragmentOne
in anActivity
. FragmentOne
holds aButton
. When clicked, it launchesFragmentTwo
, which is added to theFragment
backstack.FragmentTwo
has aButton
, which when clicked adds two tabs to theActionBar
linked to twoFragments
,FragmentThree
andFragmentFour
.FragmentThree
is visible.- If I now click the back button, I expected to see
FragmentTwo
. Instead, I seeFragmentOne
. Where didFragmentTwo
go?
在我重写的onkeydown()
,并开始实施自己的backstack的片段
我想问问是否有一些明显的我失踪?请注意,没有配置更改测试这个时候发生了。
Before I override onKeyDown()
and start implementing my own backstack for Fragments
I wanted to ask if there is something obvious I am missing? Note there is no configuration change happening when testing this.
详细信息:
FragmentOne的按钮单击处理程序包含:
FragmentOne's button click handler contains:
FragmentTransaction ft = getFragmentManager().beginTransaction();
FragmentTwo fragment = new FragmentTwo();
ft.addToBackStack(null);
ft.replace(android.R.id.content, fragment).commit();
FragmentTwo按一下按钮将在活动处理:
FragmentTwo button click is handled in the Activity:
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
FragmentFour fragmentThree = new FragmentThree();
FragmentFive fragmentFive = new FragmentFive();
ActionBar.Tab tab = getActionBar().newTab().setText("Frag 3").setTabListener(new CustomTabListener<FragmentThree>(fragmentThree));
getActionBar().addTab(tab);
tab = getActionBar().newTab().setText("Frag 4").setTabListener(new CustomTabListener<FragmentFour>(fragmentFour));
getActionBar().addTab(tab);
,其中选项卡监听器:
where the tab listener is:
public static class CustomTabListener<T extends Fragment> implements TabListener {
Fragment fragment;
public CustomTabListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(android.R.id.content, fragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
我如果添加更多的片段
在 FragmentThree
来backstack显示,它始终是只有片段
立即 FragmentThree
已经消失。
I if add more Fragments
to backstack before FragmentThree
is shown, it is always and only the Fragment
immediately before FragmentThree
that has disappeared.
当我离开由$ P $选项卡式用户视图pssing返回键,返回 FragmentOne
,标签仍显示。我明白我需要重新设置动作条
到 NAVIGATION_MODE_STANDARD
,但目前还不清楚为什么 FragmentOne
正呈现>而不是 FragmentTwo
When I leave the Tabbed user view by pressing the back key and return to FragmentOne
, the tabs are still showing. I understand I need to reset the ActionBar
to NAVIGATION_MODE_STANDARD
, but it's not clear why FragmentOne
is showing instead of FragmentTwo
.
推荐答案
我认为这个问题是如果你建立你的标签
I think the problem is if you build your tabs
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(android.R.id.content, fragment);
}
被调用和片段之前不加入到backstack
gets called and the fragment before is not added to the backstack
你尝试调用
ft.addToBackStack(null);
在片段的两个按钮处理code。
in the Fragment Two button handling code.
但你需要执行 onBack pressed()
反正摆脱标签。我想我会作出新的活性标签。
But you need to implement onBackPressed()
anyway to get rid of the tabs. I think I would make new activity with tabs.
这篇关于从backstack片段消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!