我已经实现了 ActionbarSherlock 的示例 com.actionbarsherlock.sample.fragments 并且在所选选项卡具有子片段时更改设备方向之前一切正常。
都好:
Tab1 -> 片段 1
Tab2 -> 片段2
Tab3 -> 片段 3
一切都很好,可以毫无问题地旋转设备。现在,如果我在 Fragment2 中选择一个列表项来推送 Fragment2Child1,在我旋转设备之前一切都很好。
设备旋转时不好:
Tab1 -> 片段 1
Tab2 -> Fragment2 -> Fragment2Child1
Tab3 -> 片段 3
此时选项卡和片段被重新创建,但 Fragment2 显示在 Fragment2Child1 下。当您选择另一个选项卡时情况会变得更糟,此时 Fragment2 已分离,但 Fragment2Child1 显示在新选择的选项卡片段下。我有点理解这里的机制,但我不知道如何在旋转后不附加 Fragment2,然后在选择另一个选项卡时分离 Fragment2Child1(或任何片段)。
来自扩展 SherlockFragmentActivity 的 MainFragment
…</p>
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 {
…</p>
从 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();
}