0

好的,我已经声明无故删除了片段。在我看来,就像这样,但肯定有一个我不知道的原因。请帮我找到它。

我得到了FragmentActivitywhich contains TabHost,有 5 个标签。TabHost有一个onTabChangedListener这样的(这里没什么复杂的,只是一个复制代码的例子):

new OnTabChangeListener() {
            @Override
            public void onTabChanged(String arg0) {
                LiveTabFragment fragment = null;
                int viewHolder = -1;
                switch (tabHost.getCurrentTab()) {
                case TAB_ABOUT:
                    fragment = (LiveAboutFragment) getSupportFragmentManager().findFragmentByTag(LiveAboutFragment.class.getSimpleName());
                    if (fragment == null) fragment = new LiveAboutFragment();
                    viewHolder = R.id.live_tab_about;
                    break;
                case TAB_EVENTS:
                    fragment = (LiveEventsFragment) getSupportFragmentManager().findFragmentByTag(LiveEventsFragment.class.getSimpleName());
                    if (fragment == null) fragment = new LiveEventsFragment();
                    viewHolder = R.id.live_tab_events;
                    break;
                case TAB_PLAYERS:
                    fragment = (LivePlayersFragment) getSupportFragmentManager().findFragmentByTag(LivePlayersFragment.class.getSimpleName());
                    if (fragment == null) fragment = new LivePlayersFragment();
                    viewHolder = R.id.live_tab_players;
                    break;
                case TAB_LEGEND:
                    fragment = (LiveLegendFragment) getSupportFragmentManager().findFragmentByTag(LiveLegendFragment.class.getSimpleName());
                    if (fragment == null) fragment = new LiveLegendFragment();
                    viewHolder = R.id.live_tab_legend;
                    break;
                case TAB_CHAT:
                    fragment = (LiveChatFragment) getSupportFragmentManager().findFragmentByTag(LiveChatFragment.class.getSimpleName());
                    if (fragment == null) fragment = new LiveChatFragment();
                    viewHolder = R.id.live_tab_chat;
                    break;
                }

                if (fragment != null) injectInnerFragment(fragment, viewHolder, false);
            }
        }

这是一个injectInnerFragment方法:

public void injectInnerFragment(AaFragment fragment, int viewHolderId, boolean addToBackStack) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();

    if (fragment.isAdded()) {
        transaction.replace(viewHolderId, fragment, fragment.getClass().getSimpleName());
        if (addToBackStack) transaction.addToBackStack(null);
        transaction.commit();
    } else {
        transaction.add(viewHolderId, fragment, fragment.getClass().getSimpleName());
        if (addToBackStack) transaction.addToBackStack(null);
        transaction.commit();
    }       
}

现在的问题: 当我第一次单击选项卡时,片段正在被创建(onCreate被称为),这是正常的。在大多数情况下,第二次单击已创建的选项卡片段不会调用onCreate我想要的片段。这就是为什么我FragmentManager要先找到片段。

有两种情况它不起作用,并且再次创建先前创建的片段,这对我来说效率不高。案例有:

  • 如果我单击任何选项卡,然后单击 TAB_ABOUT,然后再次单击任何选项卡会导致它重新创建(在 中找不到“任何选项卡片段” FragmentManager
  • 如果我单击任何选项卡,然后单击 TAB_CHAT,然后再次单击任何选项卡会导致它重新创建(在 中找不到“任何选项卡片段” FragmentManager

这是什么魔法?它是依赖于片段“权重”的一些自动内存释放吗?也许我应该存储所有我不想在每次在 Activity 上创建片段时重新加载的数据?

4

2 回答 2

0

通过修改注入方法解决了这个问题:

public void injectInnerFragment(AaFragment fragment, int viewHolderId, boolean addToBackStack) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();

    if (fragment.isAdded()) {
        transaction.attach(fragment);
        transaction.commit();
    } else {
        transaction.add(viewHolderId, fragment, fragment.getClass().getSimpleName());
        if (addToBackStack) transaction.addToBackStack(null);
        transaction.commit();
    }       
}
于 2013-09-27T10:56:20.777 回答
0

查看您的以下代码:

    transaction.replace(viewHolderId, fragment, fragment.getClass().getSimpleName());

尝试:

     transaction.add(viewHolderId, fragment, fragment.getClass().getSimpleName());
于 2013-09-27T09:52:11.040 回答