我正在尝试从容器中移除或替换碎片。该片段是在活动的onCreate()上创建的,一旦单击按钮,该片段应会消失。但是,我无法删除或替换该片段。
出于绝望,我尝试将容器替换为背景色相同的空白片段,但是空白片段只会显示在另一个片段下方,而不是被替换。我有一个清除所有片段的方法,该片段适用于除dashboardInfoFragment之外的所有实例。我不确定这是因为我是在onCreate()中创建它还是其他我无法识别的东西。与使用片段事务添加时一样,使用替换得到的结果相同。我从未能够从此应用程序中删除单个片段,但clearFragments()可用于其他所有片段。
我已经浏览了无数关于该主题的帖子,并尝试了所有方法,但是我无法通过引用该片段或通过标记或在clearFragments()中删除此片段,因此,我们将不胜感激。
以下是活动的相关片段
public class DashboardActivity extends AppCompatActivity {
DashboardInfoFragment dashboardInfoFragment;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
dashboardInfoFragment = DashboardInfoFragment.newInstance();
replaceFragment(R.id.dashboard_table_fragment_place, dashboardInfoFragment, "dashboardInfoFragment");
menuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isClassMenuOpen) {
startClassMenu();
} else {
clearAllFragments();
startClassMenu();
}
}
});
homeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearAllFragments();
replaceFragment(R.id.dashboard_table_fragment_place, dashboardInfoFragment, "dashboardInfoFragment");
}
});
}
public void startClassMenu() {
clearAllFragments();
// This doesn't work
removeSingleFragment(R.id.dashboard_table_fragment_place, dashboardInfoFragment, "dashboardInfoFragment");
dashboardInfoFragment = DashboardInfoFragment.newInstance();
replaceFragment(R.id.dashboard_table_fragment_place, dashboardInfoFragment, "dashboardInfoFragment");
// Trying to replace with blank fragment but gets displayed below the dashboardInfoFragment
blankFragment = BlankFragment.newInstance();
replaceFragment(R.id.dashboard_table_fragment_place, blankFragment, "blankFragment");
ClassMenuFragment classMenuFragment = ClassMenuFragment.newInstance(classList);
replaceFragment(R.id.class_menu_fragment_place, classMenuFragment, "classMenuFragment");
}
// Removes all fragments except for dashboardInfoFragment
public void clearAllFragments() {
try {
FragmentManager manager = getSupportFragmentManager();
manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
isClassMenuOpen = false;
isSettingsMenuOpen = false;
isModuleOptionsMenuOpen = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void replaceFragment(int id, Fragment fragment, String tag) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(id, fragment);
transaction.addToBackStack(tag);
transaction.commit();
}
// Some of my attempts at removing the fragment
public void removeSingleFragment(int container, Fragment fragment, String tag) {
// if (getSupportFragmentManager().findFragmentById(container) != null) {
// getSupportFragmentManager()
// .beginTransaction().
// remove(getSupportFragmentManager().findFragmentById(container)).commit();
// }
//
// getSupportFragmentManager()
// .beginTransaction()
// .replace(container, fragment)
// .commit();
//FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// if (fragment != null) {
// transaction.remove(fragment);
// transaction.commit().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// fragment = null;
//
// }
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
if(fragment!=null&&fragment.isAdded()) {
transaction = fragmentManager.beginTransaction();
transaction.remove(fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
transaction.commitAllowingStateLoss();
}
transaction = fragmentManager.beginTransaction();
transaction.replace(container, fragment, tag).commit();
}
}
});
}