选项卡在翻译后从fragmenttabhost中消失

时间:2014-07-15 13:30:20

标签: android android-fragments

我正在使用FragmentTabHost创建标签。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabwidget);
        // mTabHost = new FragmentTabHost(this);
        // mTabHost.setup(this, getSupportFragmentManager(),
        // R.id.menu_settings);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        Resources res = getResources();
        // first tab
        Bundle b = new Bundle();
        b.putString("key", url1);
        mTabHost.addTab(
                mTabHost.newTabSpec("vipusk").setIndicator(getTabIndicator(0)),
                VypuskTabFragment.class, b);
        // second tab
        b = new Bundle();
        b.putString("key", url2);
        TabSpec newTab = mTabHost.newTabSpec("constructor");
        newTab.setIndicator((getTabIndicator(1)));
        mTabHost.addTab(newTab, ConstructorTabFragment.class, b);
        // third tab
        b = new Bundle();
        b.putString("key", url3);
        mTabHost.addTab(
                mTabHost.newTabSpec("popular").setIndicator(getTabIndicator(2)),
                PopularTabFragment.class, b);
        // forth tab
        b = new Bundle();
        b.putString("key", url4);
        mTabHost.addTab(
                mTabHost.newTabSpec("actual").setIndicator(getTabIndicator(3)),
                ActualTabFragment.class, b);
        mTabHost.setOnTabChangedListener(tabChanged);
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true).cacheOnDisk(true).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext()).defaultDisplayImageOptions(
                defaultOptions).build();
        ImageLoader.getInstance().init(config); // Do it on Application start
    }

然后在标签片段中的某个点我调用另一个

Fragment details = new Fragment1();
                FragmentTransaction transaction = getFragmentManager()
                        .beginTransaction();

                Bundle bundles = new Bundle();
                bundles.putSerializable("key", articles.get(position).getLink()
                        + "?templateStyle=19");
                details.setArguments(bundles);
                transaction.replace(android.R.id.content, details);
                transaction.addToBackStack(null);
                transaction.commit();
在那次转换之后,我的tabwidget消失了。据我所知,我执行的事务类型替换整个fragmenttabhost而不是单个选项卡的内容。我该如何避免这种行为?

编辑: 如果我替换

transaction.replace(android.R.id.content, details);

通过

  transaction.replace(R.id.realtabcontent, details);

我会得到理想的行为,但在返回导航时我必须重新加载以前的片段,这是不希望的

1 个答案:

答案 0 :(得分:0)

错误是由顶级片段的替换引起的。而是在标签片段中创建额外的容器并将其用于内容片段替换