避免在android中重新创建FragmentTabHost的TabChnage标签

时间:2015-03-22 02:21:47

标签: android fragment-tab-host

我正在使用FragmentTabHost进行简单的两个标签设置。但是,当我从一个切换到另一个时,会重新创建选项卡。如何避免这种情况。虽然我试图隐藏/显示,但没有工作,请参阅下面的updateTab方法。

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
   {

    mTabHost = new FragmentTabHost(getActivity());
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabhost);

    mTabHost.addTab(
            mTabHost.newTabSpec("tabAchievements").setIndicator(
                    getResources().getString(R.string.tab_achievements),
                    getResources().getDrawable(R.drawable.location)),
            CompleteFragment.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tabIncomplete").setIndicator(
                    getResources().getString(R.string.tab_incomplete),
                    getResources().getDrawable(R.drawable.eye)),
            IncompleteFragment.class, null);

    myLastTag = "tabAchievements";
    mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

        public void onTabChanged(String tabId) {
            if (tabId.equals("tabAchievements")) {

                updateTab(tabId, R.string.tab_achievements);
                return;
            }
            if (tabId.equals("tabIncomplete")) {
                updateTab(tabId, R.string.tab_incomplete);
                return;
            }
        }
    });
    return mTabHost;
}

private void updateTab(String tabId, int placeholder) {
    Fragment newTab = getChildFragmentManager().findFragmentByTag(tabId);
    Fragment mLastTab = getChildFragmentManager().findFragmentByTag(
            myLastTag);
    myLastTag = tabId;
    if (mLastTab != newTab) {
        FragmentTransaction ft = activity.getSupportFragmentManager()
                .beginTransaction();
        if (mLastTab != null) {
            if (mLastTab != null) {
                ft.hide(mLastTab);
            }
        }
        if (newTab == null) {
            if (tabId == "tabAchievements")
                newTab = Fragment.instantiate(activity,
                        CompleteFragment.class.getName(), null);
            else
                newTab = Fragment.instantiate(activity,
                        IncompleteFragment.class.getName(), null);
            ft.add(android.R.id.tabcontent, newTab, null);

        } else {
            ft.show(newTab);
        }
        ft.commit();
    }
}

1 个答案:

答案 0 :(得分:3)

我有同样的问题,最后解决了。覆盖OnTabChangeListener是不够的,因为FragmentTabHost在调用侦听器之前首先在内部执行片段的附加或分离,因此仍然会调用OnCreateView的生命周期。看看这个解决方案,How to maintain fragment's state in the application,它对我有用。下载最后一个FragmentTabHost.java代码并替换doTabChanged(我还在show之前添加了对onPause的调用,并在show之后添加了onResume,否则它们不会被调用):

private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
    TabInfo newTab = null;
    for (int i=0; i<mTabs.size(); i++) {
        TabInfo tab = mTabs.get(i);
        if (tab.tag.equals(tabId)) {
            newTab = tab;
        }
    }
    if (newTab == null) {
        throw new IllegalStateException("No tab known for tag " + tabId);
    }
    if (mLastTab != newTab) {
        if (ft == null) {
            ft = mFragmentManager.beginTransaction();
        }
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                mLastTab.fragment.onPause();
                ft.hide(mLastTab.fragment);
            }
        }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(mContext,
                        newTab.clss.getName(), newTab.args);
                ft.add(mContainerId, newTab.fragment, newTab.tag);
                findViewById(mContainerId).setContentDescription("DEBUG. add fragment to this container");
            } else {
                if (newTab.fragment.isHidden()){
                    ft.show(newTab.fragment);
                    newTab.fragment.onResume();
                }
                else{
                    ft.attach(newTab.fragment);
                }
            }
        }

        //mPreviousTab = mLastTab;
        mLastTab = newTab;
    }
    return ft;

}

并且不要覆盖OnTabChangedListener。您当然要使用包路径更改对android.support.v4.app.FragmentTabHost的引用,并在Layout文件中也这样做...