我无法理解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
。
我如何更换特定片段?
答案 0 :(得分:0)
问题似乎与 R.id.fragment_container 有关。你应该在下面的行中有一个不同的第一个参数,以便替换不是你的"第一个"片段:
manager.add(R.id.fragment_container, second_fragment,"second");