问题
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