片段替换()不替换所有片段(Fragment replace() not replacing all

2019-09-16 10:25发布

如果我请添加()为片段A和B具有相同的viewId然后尝试调用替换()与C片段即viewId,仅片段A是越来越除去,用片段B和C根据该文档结束了, A和B都应该用C来代替......还是我读文档错了吗?

这里有一个组合,这是否:

public class FragmentActivity extends SherlockFragmentActivity {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        getSupportFragmentManager().beginTransaction().add(R.id.fragment, new FragmentA()).add(R.id.fragment, new FragmentB()).commit();

        ((Button) findViewById(R.id.swap)).setOnClickListener(new View.OnClickListener() {          
            @Override
            public void onClick(final View view) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment, new FragmentC()).commit();
            }
        });
    }
}

Answer 1:

综观文档,.replace调用需要一个片段作为一个参数的方法。 所以我猜想,这只是为了更换一个片段。 我真的不明白,你为什么会在第一时间加入两个片段相同的ID。



文章来源: Fragment replace() not replacing all fragments