FragmentManager clean up for .addToBackStack



无论如何我可以让这个更干净吗?我知道当查询="我在主菜单中,因此后面应该关闭应用程序。

        if (query == "") {
            fragmentManager.beginTransaction()
                    .replace(containerId, fragment, tag)
                    .commitAllowingStateLoss();
        } else {
            fragmentManager.beginTransaction()
                    .replace(containerId, fragment, tag)
                    .addToBackStack(null)
                    .commitAllowingStateLoss();
        }

是的,您可以一一清理片段,这是代码...

   public static void clearBackStack() {
        FragmentManager fm = mainActivity.getSupportFragmentManager();
        for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
             fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }

好吧,这对我来说更好。虽然我感谢上面的答案。

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(containerId, fragment, tag);
     if (query != "") {
         transaction.addToBackStack(null);
     }
     transaction.commitAllowingStateLoss();

最新更新