从backstack片段消失 [英] Fragment disappears from backstack

查看:138
本文介绍了从backstack片段消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 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 called FragmentOne in an Activity.
  • FragmentOne holds a Button. When clicked, it launches FragmentTwo, which is added to the Fragment backstack.
  • FragmentTwo has a Button, which when clicked adds two tabs to the ActionBar linked to two Fragments, FragmentThree and FragmentFour. FragmentThree is visible.
  • If I now click the back button, I expected to see FragmentTwo. Instead, I see FragmentOne. Where did FragmentTwo 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片段消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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