我已经实现了ActionbarSherlock的示例com.actionbarsherlock.sample.fragments,并且在选定的选项卡具有子片段时更改设备方向之前,所有方法都工作良好。

都好:

Tab1-> Fragment1

Tab2-> Fragment2

Tab3-> Fragment3

一切都很好,并且可以毫无问题地旋转设备。现在,如果我在Fragment2中选择一个列表项来推送Fragment2Child1,则在旋转设备之前一切都还不错。

设备旋转时效果不佳:

Tab1-> Fragment1

Tab2-> Fragment2-> Fragment2Child1

Tab3-> Fragment3

此时,将重新创建选项卡和片段,但Fragment2将显示在Fragment2Child1下。当您选择另一个选项卡时,情况变得更糟,这时Fragment2被分离,但是Fragment2Child1显示在新选择的选项卡片段下。我有点了解这里的机制,但是我无法弄清楚如何在旋转后不附加Fragment2,然后在选择另一个选项卡时分离Fragment2Child1(或任何与此相关的片段)。

从MainFragment扩展了SherlockFragmentActivity



int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {

            mTabManager.addTab(mTabHost.newTabSpec("new").setIndicator(getString(R.string.new_)), NewListSupportActivity.NewListFragment.class, null);
        mTabManager.addTab(mTabHost.newTabSpec("project").setIndicator(getString(R.string.project)), ProjectSupportActivity.ProjectListFragment.class, null);
        mTabManager.addTab(mTabHost.newTabSpec("setting").setIndicator(getString(R.string.settings)), SettingSupportActivity.SettingListFragment.class, null);

        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }

    } else {




从TabManager类

public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {

    tabSpec.setContent(new DummyTabFactory(this.mActivity));
    final String tag = tabSpec.getTag();

    final TabInfo info = new TabInfo(tag, clss, args);

    // Check to see if we already have a fragment for this tab, probably
    // from a previously saved state. If so, deactivate it, because our
    // initial state is that a tab isn't shown.
    info.fragment = this.mActivity.getSupportFragmentManager().findFragmentByTag(tag);
    if (info.fragment != null && !info.fragment.isDetached()) {
        final FragmentTransaction ft = this.mActivity.getSupportFragmentManager().beginTransaction();
        ft.detach(info.fragment);
        ft.commit();
    }

    this.mTabs.put(tag, info);
    this.mTabHost.addTab(tabSpec);
}

@Override
public void onTabChanged(String tabId) {

    TabInfo newTab = mTabs.get(tabId);
    if (mLastTab != newTab) {
        FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                ft.detach(mLastTab.fragment);
            }
        }
        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.attach(newTab.fragment);
            }
        }

        mLastTab = newTab;
        ft.commit();
        mActivity.getSupportFragmentManager().executePendingTransactions();
    }
}


以及将Fragment2Child1或任何子对象推送到该代码的代码

public void pushFragment(TradiesFragment current, TradiesFragment fragment) {

    fragment.setFragmentListener(this);
    final FragmentManager fm = this.fragment.getFragmentManager();
    final FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    ft.detach(current);
    ft.add(this.fragment.getId(), fragment);
    ft.addToBackStack(null);
    ft.commit();
}

最佳答案

这个问题没有答案。至少我能想到的都不是丑陋的。解决方案是将Fragment2Child1包装在一个Activity中。导航到Fragment2Child1Activity时,您最终隐藏了选项卡活动。并且每次用户都被迫导航回主要活动(带有选项卡)。从好的方面来说,它实际上使UI更加简单。

09-25 20:39