两天前我发布了一个问题:TabHost中的新布局。所以我决定使用片段创建选项卡,而不是使用tabActivity。现在我有3个选项卡:tab_clients、tab_settings、tab_logut。当我运行我的应用程序时,tab1被显示出来,如果我旋转我的设备,一切都正常。但是当我从这些选项卡中切换到tab2或tab3时,当我旋转我的设备时,我得到了tab1的视图。这是我的主要容器:
public class AplicacionActivity extends 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();
}
当我点击其中一个选项卡内的按钮并替换布局时,同样的情况也会发生。当我旋转时,我会看到选项卡1的视图。我必须说,我运行过使用tabActivity而不是fragments的应用程序,并且没有这个问题。所以我有点困惑。
任何帮助将不胜感激。
编辑:解决方案如下:
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());
}
正如您所看到的,我在bundle中保存了旋转设备之前我看到的片段标签。
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 的帮助 :)
solved
(接受一个答案就足够了)或者Android。 - user