无法理解片段替换

时间:2015-04-03 13:36:15

标签: android

我无法理解FragmentTransaction.replace(id, fragment, 'string') .replace实际

的内容

我在某处读到它会用片段替换视图id的内容但在我的

public class MainActivity extends 
    @Override
    public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                if (findViewById(R.id.fragment_container) !=null) {
                    if(savedInstanceState!=null){
                        return ;
                    }
                    ArticleFragment first_fragment = new ArticleFragment();
                    HeadlineFragment second_fragment = new HeadlineFragment();
                    first_fragment.setArguments(getIntent().getExtras());
                    second_fragment.setArguments(getIntent().getExtras());
                    FragmentTransaction manager=getSupportFragmentManager().beginTransaction();
                    manager.add(R.id.fragment_container, first_fragment,"first");
                    manager.add(R.id.fragment_container, second_fragment,"second");
                    manager.commit();
                }
                abc newFragment = new abc();
                Bundle args = new Bundle();
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, newFragment,"second");
                transaction.addToBackStack(null);
                transaction.commit();
            }
        }

它只替换我添加到Transaction的第一个片段,即first_fragment

我如何更换特定片段?

1 个答案:

答案 0 :(得分:0)

问题似乎与 R.id.fragment_container 有关。你应该在下面的行中有一个不同的第一个参数,以便替换不是你的"第一个"片段:

manager.add(R.id.fragment_container, second_fragment,"second");