显示隐藏的片段

我知道有很多这样的问题。但是我的选择正在发生一种奇怪的情况。好吧,这不是很清楚。您可以判断出什么问题。

I hide activeFragment if it is not null. Then I add a Fragment if it is not. And if there is, then I show. From the first time, everything works fine when I create it using the add() method, but if I go back and try to show() the already hidden Fragment, then it's just a white screen, there is no Fragment. What could be the problem?

private void loadFragment(Fragment fragment, String tag) {
    if (fragment != null) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        if (activeFragment != null) {
            transaction.hide(activeFragment);
        }

        if (fm.findFragmentByTag(tag) == null) {
            transaction.add(R.id.fragment_container, fragment, tag);
        } else {
            transaction.show(fragment);
        }
        activeFragment = fragment;

        transaction.commit();
    }
}
评论