FragmentTransaction.remove没有效果 [英] FragmentTransaction.remove has no effect

查看:1480
本文介绍了FragmentTransaction.remove没有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求很简单:我有一个按钮,应该更换碎裂通过FragmentB

这听起来很容易,几乎工作中,最大的问题是,旧的片段不会被删除,新的放置在旧的前面,他们是生活在一起,我的布局。

的code:

  FragmentManager fragMgr = a.getSupportFragmentManager();
片段currentFragment =(片段)fragMgr.findFragmentById(R.id.fragmentitself);

如果(currentFragment!= NULL){

     FragmentTransaction fragTrans = fragMgr.beginTransaction();
     fragTrans.remove(currentFragment);

     FragmentB newFragment =新FragmentB();
     fragTrans.replace(R.id.fragmentcontainer,newFragment);
     //我也试图与R.id.fragmentitself
     fragTrans.addToBackStack(空);
     fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
     fragTrans.commit();
}
 

的布局:

 <的FrameLayout
    机器人:layout_width =0dp
    机器人:layout_height =match_parent
    机器人:layout_weight =2
    机器人:ID =@ + ID / fragmentcontainer>

    <片段
        机器人:ID =@ + ID / fragmentitself
        机器人:名称=com.WazaBe.MyApp.FragmentA
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>
< /的FrameLayout>
 

解决方案

解决方案

首先,你必须从XML删除您的片断,并只保留空的容器有:

 <的FrameLayout
    机器人:layout_width =0dp
    机器人:layout_height =match_parent
    机器人:layout_weight =2
    机器人:ID =@ + ID / fragmentcontainer/>
 

然后,你需要从code,也就是增加你的 com.WazaBe.MyApp.FragmentA 片段的onCreate()。

说明

这是因为,交易你操纵的ViewGroup FrameLayouts 是容器碎片。但是,当你直接把你的片段XML布局,那么它就变成永久性 View层次结构中的一部分,因为它是永久性的,它不能从code删除。

一旦你得到你的布局固定的,删除()通话已不再需要 - 这将足以只是更换()

My requirement is quite simple: I have a button that should replace a FragmentA by FragmentB.

This sounds easy and nearly work, the big problem is that the old fragment is not removed and the new placed on the front of the old one and they are "living" together in my layout.

The Code:

FragmentManager fragMgr = a.getSupportFragmentManager();
Fragment currentFragment = (Fragment) fragMgr.findFragmentById(R.id.fragmentitself);

if(currentFragment!=null){

     FragmentTransaction fragTrans = fragMgr.beginTransaction();
     fragTrans.remove(currentFragment);

     FragmentB newFragment = new FragmentB();
     fragTrans.replace(R.id.fragmentcontainer, newFragment);
     // I have also tried with R.id.fragmentitself
     fragTrans.addToBackStack(null);
     fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
     fragTrans.commit();
}

The Layout:

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" 
    android:id="@+id/fragmentcontainer">

    <fragment
        android:id="@+id/fragmentitself"
        android:name="com.WazaBe.MyApp.FragmentA"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

解决方案

Solution

First, you have to remove your fragment from XML and just keep empty container there:

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" 
    android:id="@+id/fragmentcontainer" />

Then you need to add your com.WazaBe.MyApp.FragmentA fragment from code, i.e. in onCreate() of your parent Activity.

Explanation

It is because with transactions you manipulate ViewGroup such as FrameLayouts that is container for Fragments. But when you place your Fragment directly in XML layout, then it becomes permanent part of the View hierarchy and because it is permanent, it cannot be removed from code.

Once you get your layout fixed, the remove() call is no longer needed - it will suffice to just do replace()

这篇关于FragmentTransaction.remove没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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