Switching between fragmentTabs giving unexpected results

爱⌒轻易说出口 提交于 2019-12-03 16:09:09

Without more details it's tough to say what's wrong specificaly. However, I can say from personal experience that when I first had to implement fragment tabs, I went through a lot of lousy tutorials before finding something that worked. The tutorial that finally made sense for me is here: http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

As a bonus, there's also some Github source code: https://github.com/mitchwongho/Andy/tree/master/Andy/src/com/andy/fragments/tabs

Update:

I recommend you retain the original code for onTabChanged and use addToBackStack method for maintaining the traversal state of fragments. Call addToBackStack when going from one fragment to next, like when adding or replaceing fragments.

Also change the TabInfo.fragment reference to reflect the transitions between fragments inside a tab.


Do not attach and detach every time tab is changed.

   @Override
    public void onTabChanged(String tabId) {
        TabInfo newTab = mTabs.get(tabId);
        if (mLastTab != newTab) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();

            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(mActivity,
                            newTab.clss.getName(), newTab.args);
                    ft.add(mContainerId, newTab.fragment, newTab.tag);
                } else {
                    ft.replace(mContainerId, newTab.fragment, newTab.tag);
                } 
            }
            mLastTab = newTab;
            ft.commit();
            mActivity.getSupportFragmentManager().executePendingTransactions();
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!