我有两个窗格布局,左侧有按钮。当按下按钮时,其对应的片段显示在右侧。有时片段重叠,但这是一个间歇性问题。我不能一直复制它,但它确实发生了
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButtonSettingsManageBooks:
SettingsManageBooksFragment mbFragment = new SettingsManageBooksFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, mbFragment).addToBackStack(null).commit();
mImgFragmentTitle.setImageResource(R.drawable.manage_my_books);
this.getSupportFragmentManager().executePendingTransactions();
break;
case R.id.imageButtonSettingsPurchaseHistory:
SettingsPurchaseHistoryFragment phFragment = new SettingsPurchaseHistoryFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, phFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.purchase_history);
this.getSupportFragmentManager().executePendingTransactions();
break;
case R.id.imageButtonSettingsAudio:
SettingsAudioFragment aFragment = new SettingsAudioFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, aFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.audio);
this.getSupportFragmentManager().executePendingTransactions();
break;
case R.id.imageButtonSettingsRestore:
SettingsRestoreFragment rFragment = new SettingsRestoreFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, rFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.restore);
this.getSupportFragmentManager().executePendingTransactions();
break;
case R.id.imageButtonSettingsCopyright:
SettingsCopyrightFragment cFragment = new SettingsCopyrightFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, cFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.copyright);
this.getSupportFragmentManager().executePendingTransactions();
break;
case R.id.imageButtonSettingsAbout:
SettingsAboutFragment abFragment = new SettingsAboutFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, abFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.about);
this.getSupportFragmentManager().executePendingTransactions();
break;
看起来是因为我用可能导致重叠的新片段替换每个片段。有没有办法在提交新碎片之前清除所有碎片?