我在app中遇到此问题,其中下面的方法并不总是有效地隐藏当前可见的片段(contentFragment)并显示新的片段(片段)。基本上,有时,它与它应该做的完全相反,也就是说,当前片段保持可见,而新片段根本没有显示。值得的是,当它发生时我不能总是复制。
我真的很有智慧。有人有任何想法吗?
public void addContentFragment(GenericFragment fragment) {
FragmentManager fragmentManager = slidingMenusActivity.getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.ContentLayout, fragment, fragment.getFragmentTag());
transaction.addToBackStack(fragment.getFragmentTag());
Log.d(TAG, "addContentFragment hiding contentFragment=" + contentFragment);
transaction.hide(contentFragment);
transaction.commit();
fragmentManager.executePendingTransactions();
contentFragment = fragment;
}
答案 0 :(得分:1)
您的 contentFragment 有时没有您期望的值,请尝试检索当前片段以确保您在新事务之前隐藏片段。
修改您的addContentFragment(GenericFragment fragment)
,如下所示:
public void addContentFragment(GenericFragment fragment) {
FragmentManager fragmentManager = slidingMenusActivity.getSupportFragmentManager();
Fragment currentFragment = fragmentManager.findFragmentById(R.id.ContentLayout);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.ContentLayout, fragment, fragment.getFragmentTag());
transaction.addToBackStack(fragment.getFragmentTag());
Log.d(TAG, "addContentFragment hiding contentFragment=" + contentFragment);
transaction.hide(currentFragment);
transaction.commit();
}