片段相互重叠 [英] Fragments overlapping each other

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

问题描述

我有3个标签的操作栏,每个选项卡中打开一个片段。第三个选项卡,目录,有一个列表:

I have an action bar with 3 tabs, each tab opens a fragment. The third tab, "Catalog", has a list:

当我点击它打开另一个片段中的项目,这是不是在操作栏的一部分:

When I click on an item it opens another fragment, which is not part of the action bar:

public void onClick(View v) {
    switch (v.getId())
    {
    case R.id.category1:    
        Fragment cosmeticsFragment = new ActivityCosmetics();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        transaction.replace(android.R.id.content, cosmeticsFragment);
        transaction.addToBackStack(null);

        transaction.setTransition(1);

        transaction.commit();
        break;
        ...

这是什么样子后认为:

This is what it looks like after that:

从这个角度,如果我去其他选项卡,然后返回到目录选项卡,我看到2 previous片段相互重叠的:

From this point, if I go to other tab and then return to the Catalog tab, I see the 2 previous fragments overlapping each other:

如何发生做我prevent呢?

How do I prevent it from happening?

推荐答案

您可以通过标签搜索他们管理的片段。当添加片段backstack添加标签名称

You can manage your fragments by searching them by tag. When adding fragment to backstack add TAG name

transaction.addToBackStack("myCustomFragmentTag");

如果你想随时随地消灭片段中的应用:

If you want to destroy Fragment anywhere in application :

Fragment previousInstance = getFragmentManager().findFragmentByTag("myCustomFragmentTag");
                if (previousInstance != null)
                    transaction.remove(previousInstance);

您可以尝试改写某些行为,所以这行code会剿初始化的最后一个片段

You can try override some behavior so this line of code 'll destroy initialized last Fragment

getFragmentManager().popBackStack(); 

这篇关于片段相互重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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