我已经实现了ActionbarSherlock的示例com.actionbarsherlock.sample.fragments,并且一切正常,直到所选选项卡包含子片段时更改设备方向。
一切都好:
Tab1 - >片段1
Tab2 - > Fragment2
Tab3 - > Fragment3
一切都很好,可以毫无问题地旋转设备。现在,如果我在Fragment2中选择一个列表项来推送Fragment2Child1,那么在我旋转设备之前,一切都还是不错的。
设备旋转时效果不佳:
Tab1 - >片段1
Tab2 - > Fragment2 - >的 Fragment2Child1
Tab3 - > Fragment3
此时重新创建选项卡和片段,但片段2显示在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();
}
答案 0 :(得分:1)
这个问题没有答案。至少没有一个我能想到的并不是完全丑陋的。解决方案是将 Fragment2Child1 包装在Activity中。在导航到 Fragment2Child1Activity 时,您最终会隐藏选项卡活动。用户每次都被迫导航回主要活动(带标签)。从好的方面来说,它实际上是一个更简单的用户界面。