2天前我发了一个问题:New layouts in 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获得的帮助:)
答案 0 :(得分:1)
在方向更改时,将重新创建活动。您可能需要一个跟踪活动选项卡的变量
有关处理配置更改的更多信息,请参阅Android指南: http://developer.android.com/guide/components/activities.html#ConfigurationChanges