gpt4 book ai didi

android - 改变方向时 fragment 消失

转载 作者:太空狗 更新时间:2023-10-29 14:21:35 24 4
gpt4 key购买 nike

2 天前我发布了一个问题:New layouts in TabHost .所以我决定用 fragment 而不是 tabActivity 创建选项卡。所以现在我有 3 个选项卡:tab_clients、tab_settings、tab_logut。当我运行我的应用程序时,会显示 tab1,如果我旋转我的设备,一切正常。当我更改为 tab2 或 tab3 时,我感到很惊讶,当我旋转我的设备时,我从这些选项卡中看到了 tab1 的 View 。这是我的主要容器:

公共(public)类 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 的 View 。我不得不说我已经运行了使用 tabActivity 而不是 fragment 的应用程序并且没有这个问题。所以我在这里有点困惑。

我们将不胜感激。

已编辑:解决方案是:

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 中保存了我在旋转设备之前看到的 fragment 标签。

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();
}

就是这样!每次我改变设备的方向时,我都不会丢失 Activity 布局!非常感谢@Yuri 给我的帮助 :)

最佳答案

在方向改变时重新创建一个 Activity 。您可能需要一个变量来跟踪 Activity 选项卡

  • 将变量的值保存在onSaveInstanceState()中
  • 从 savedInstanceState Bundle 恢复 onCreate() 中的值。
  • 使用恢复的值正确初始化标签

有关处理配置更改的更多信息可以在 Android 指南中找到: http://developer.android.com/guide/components/activities.html#ConfigurationChanges

关于android - 改变方向时 fragment 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16429982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com