2 天前我发布了一个问题:TabHost 中的新布局。所以我决定用片段而不是tabActivity创建标签。所以现在我有 3 个选项卡:tab_clients、tab_settings、tab_logut。当我运行我的应用程序时,会显示 tab1,如果我旋转我的设备,一切正常。当我更改为 tab2 或 tab3 时,我感到很惊讶,当我旋转设备时,从这些选项卡中可以看到 tab1 的视图。这是我的主要容器:
公共类 AplicacionActivity 扩展 FragmentActivity {
/* Tab identif */
static String TAB_clientes = "Clientes";
static String TAB_settings = "Settings";
static String TAB_logout = "Logout";
TabHost mTabHost;
ClientesActivity fragmentClientes;
SettingsActivity fragmentSettings;
LogoutActivity fragmentLogout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aplicacion);
fragmentClientes = new ClientesActivity();
fragmentSettings = new SettingsActivity();
fragmentLogout = new LogoutActivity();
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setOnTabChangedListener(listener);
mTabHost.setup();
iniTab();
}
public void iniTab() {
Resources res = getResources();
//TabHost.TabSpec: A tab has a tab indicator, content, and a tag that is used to keep track of it.
TabHost.TabSpec spec = mTabHost.newTabSpec(TAB_clientes);
mTabHost.setCurrentTab(-3);
//TabHost.TabContentFactory: Makes the content of a tab when it is selected.
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
spec.setIndicator(" Clientes ", res.getDrawable(R.drawable.clientes));
mTabHost.addTab(spec);
spec = mTabHost.newTabSpec(TAB_settings);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
spec.setIndicator(" Settings ",res.getDrawable(R.drawable.settings));
mTabHost.addTab(spec);
spec = mTabHost.newTabSpec(TAB_logout);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
spec.setIndicator(" Logout ",res.getDrawable(R.drawable.logouticon));
mTabHost.addTab(spec);
}
/*
* TabChangeListener for change the tab when is selected
*/
TabHost.OnTabChangeListener listener = new TabHost.OnTabChangeListener() {
public void onTabChanged(String tabId) {
/*Set current tab..*/
if(tabId.equals(TAB_clientes)){
pushFragments(tabId, fragmentClientes);
}else if(tabId.equals(TAB_settings)){
pushFragments(tabId, fragmentSettings);
}else if(tabId.equals(TAB_logout)){
pushFragments(tabId, fragmentLogout);
}
}
};
/*
* insert the fragment into the FrameLayout
*/
public void pushFragments(String tag, Fragment fragment){
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(android.R.id.tabcontent, fragment);
ft.commit();
}
当我单击其中一个选项卡内的按钮并替换布局时,也会发生同样的情况。当我旋转时,我得到了 tab1 的视图。我不得不说我已经运行了使用 tabActivity 而不是片段的应用程序并且没有这个问题。所以我在这里有点困惑。
任何帮助将不胜感激。
编辑:解决方案是:
1.- 在我的 FragmentActivity 上创建以下方法:
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
Fragment fragmentAct = this.getSupportFragmentManager().findFragmentById(android.R.id.tabcontent);
bundle.putString("fragmentAct", fragmentoActual.getTag());
Log.e("layout-saving",fragmentAct.getTag());
}
如您所见,我将在旋转设备之前看到的片段标签保存在捆绑包中。
2.- 在 FragmentActivity 的 OnCreate 方法中,我添加了以下代码:
if (savedInstanceState != null){
String value = savedInstanceState.getString("fragmentAct");
establishLayout(value);
}
3.- 我创建了一个名为establishLayout 的方法:
public void establishLayout(String tagFragment){
mTabHost.setCurrentTabByTag(tagFragment);
Fragment fragmentoActual = this.getSupportFragmentManager().findFragmentByTag(tagFragment);
if(fragmentoActual!=null){
pushFragments(tagFragment, fragmentoActual);
}
}
4.- 我创建了 pushFragments 方法:
public void pushFragments(String tag, Fragment fragment){
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(android.R.id.tabcontent, fragment, tag);
ft.commit();
}
就是这样!。每次我更改设备的方向时,我都不会丢失活动布局!非常感谢@Yuri 给我的帮助 :)