Android 无法理解碎片替换
我无法理解Android 无法理解碎片替换,android,Android,我无法理解片段事务。replace(id,fragment,'string')实际上什么是。replace 我在某处读到它将用片段替换视图id的内容,但在我的 public class MainActivity extends @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
片段事务。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();
}
}
它只替换我添加到事务中的第一个片段,即第一个片段
如何替换特定片段?问题似乎在于R.id.fragment\u容器。为了替换不是“第一个”片段的片段,您应该在下面的行中使用不同的第一个参数:
manager.add(R.id.fragment_container, second_fragment,"second");
id R.id.fragment_容器不是用来指定在哪里添加片段的吗?