Fragment replace() not replacing all fragments

风格不统一 提交于 2019-12-22 08:17:31

问题


If I call add() for fragments A and B with the same viewId and then try to call replace() on that viewId with fragment C, only fragment A is getting removed, ending up with fragments B and C. According to the docs, BOTH A and B should be replaced by C...or am I reading the docs wrong?

Here's one combination that does this:

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();
            }
        });
    }
}

回答1:


Looking at the docs, .replace calls a method that takes a fragment as a parameter. So I would guess that it is only meant to replace one fragment. I don't really understand why you would add two fragments to the same id in the first place.



来源:https://stackoverflow.com/questions/10065640/fragment-replace-not-replacing-all-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!