我一直在玩Android v4支持库中的FragmentTabHost一段时间,并遇到了一个我无法解决的严重问题。我的目标要求如下。
1)Fragment
,其中还包含由Fragments
构建的标签,其中包含嵌套的Fragments
,其层次结构如下:
android.support.v4.app.Fragment
(HostFragment)
android.support.v4.app.FragmentTabHost
(TabHost)
android.support.v4.app.Fragment
(Tab1Fragment)
android.support.v4.app.Fragment
(Tab1Fragment1)android.support.v4.app.Fragment
(Tab1Fragment2)android.support.v4.app.Fragment
(Tab1Fragment3)android.support.v4.app.Fragment
(Tab2Fragment)
android.support.v4.app.Fragment
(Tab2Fragment1)android.support.v4.app.Fragment
(Tab2Fragment2) HostFragment + TabHost
设置是根据文档here中的说明完成的。
2)屏幕旋转保留状态为HostFragment
,因为此设置的重新创建是耗费资源的操作,除了布局顺序之外,此屏幕上没有任何变化,因此仅显示屏幕不需要额外的工作。只需在layout
和onCreateView
的{{1}}回调中返回不同的Tab1
,然后将现有的片段重新附加到相同的ID中。
要实现这一目标,我们会认为只设置Tab2
就能完成工作,而且部分确实如此。没有任何东西被重新创建,标签被保留,因为它们应该,一切正常。现在让我们谈谈。
问题
稍后出现了一个大问题。似乎HostFragment.setRetainInstance(true)
(以及所有其他Tab1Fragment1
)未附加到轮播时创建的新TabXFragmentY
。它们仅在Activity
的第一次运行时附加,然后当用户旋转屏幕时没有任何操作。
后果
这对我来说有两大问题(可能还有更多):
Activity
被泄露,因为Activity
中的Fragments
所有TabHost
都是无缘无故的。getActivity()
的任何一个上致电TabXFragmentY
时,您将获得旧 Activity
,并且可怕的“无法在onSavedInstance之后执行操作”异常。< / LI>
醇>
当您需要来自父Activity的内容时,这会导致严重的问题。此外,当设置HostFragment.setRetainInstance(false)
所有内容都重新创建并且工作正常时,它不会发生。
问题
这是一个我只看到的问题吗?我找不到关于这个主题的任何内容,我已经审查了我的代码一百次。
我也尝试从v4示例更改com.example.android.supportv4.app.FragmentTabsFragmentSupport
,我确实将其设置为保留它的实例,并在每个onAttach
调用的其中一个嵌套片段中添加了日志信息以验证我可以看到还有同样的问题。我很困惑。帮助
答案 0 :(得分:1)
最近,我一直在TabHost
与Fragment
一起工作并发现同样的问题。基本上,您需要控制哪些Fragment
被附加/分离,在我的情况下,我是在onTabChanged()
事件中执行此操作。
我有一个TabInfo
课程,我为每个Tab
Fragment
的名称(用于识别目的)。Fragment
。TabHost.TabSpec
规范,就好像你要删除一些标签一样,有关重新创建其余标签的信息,TabHost
对于删除标签来说有点棘手。Bundle
关联(基本上是配置更改之前保存的实例)。此外,我需要跟踪lastTab
和newTab
已打开,因为TabHost
没有本地方式知道哪些TabHost
刚刚关闭,所以我这样做声明一个变量类。这就是我现在处理的方式,我会尽量添加尽可能多的评论:
@Override
public void onTabChanged(final String tag) {
// I get the info for the Tab just triggered using its tag
final TabInfo newTab = mTabInfo.get(tag);
// If there's actually been a tab change...
if (lastTab != newTab) {
// You'll have to make a transaction for replacing the Fragment
final FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
// If the last tab actually has a Fragment associated to it
if ((lastTab != null) && (lastTab.getFragment() != null)) {
// In my case I've an additional level of complexion, as I have a nested Fragment
// inside my content Fragment. So I have to remove it first prior to detaching
// the parent Fragment. This is not needed if you have just one Fragment as content.
final Fragment loginFrag = (Fragment) lastTab.getFragment().getActivity().getSupportFragmentManager().findFragmentById(lastTab.getLoginFragId());
ft.remove(loginFrag);
// And this is what does the trick: I initially was calling detach() instead of remove()
// but seems that with some versions there's a problem that makes not apply it,
// calling remove will actually remove this Fragment
ft.remove(lastTab.getFragment());
}
// You've detached the old Fragment, you have now to attach the new one
if (newTab != null) {
if (newTab.getFragment() == null) {
// Inflate the new content if it's the first time the tab has been fired
final TabFragmentInflater tabInf = new TabFragmentInflater();
newTab.setFragment(Fragment.instantiate(this, tabInf.getClass().getName(), newTab.getArgs()));
ft.add(R.id.realtabcontent, newTab.getFragment(), newTab.getTag());
}
else {
// if not, just attach its fragment
ft.attach(newTab.getFragment());
}
}
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
lastTab = newTab;
}
}
----编辑----
回答你的问题:
我确实在newTabSpec()
上调用TabHost
,它是一个我没有包含的单独方法,因为我刚刚包含了onTabChanged()
回调。您有正常创建标签,单击标签时会触发此onTabChanged()
方法。对于创作,我做这样的事情:
private void initTabHost(final Bundle args) {
final TabHost th = (TabHost) findViewById(android.R.id.tabhost);
th.setup();
// I have a HashMap called fragMap where as the key I define the tab's name
// And as the value, I have an Integer which is a unique identifier
// to know what to inflate when I call the TabFragmentInflater (I will
// add the code below). You can perfectly add it as an id or a tag also.
for (final String tablabel : fragMap.keySet()) {
final TabHost.TabSpec tabSpec = th.get().newTabSpec(tabname).setIndicator(tabname);
// Here I initialize a TabInfo object for this tab, which will include additional
// Handling info: Name of tab, Tab Spec, The unique ID I explained above,
// the forth argument is irrelevant in your example, and args (saved instance)
final TabInfo tabInfo = new TabInfo(tabname, tabSpec, fragMap.get(tablabel), R.id.someLayout, args);
// This is not actually the TabHost's `addTab()` method, I'll call it inside
// this method (see below)
MyClass.addTab(this, th, tabSpec, tabInfo);
// I have to be able to keep tracking of that info
mTabInfo.put(tabInfo.getTag(), tabInfo);
}
// For the tab creation, I force it to start on the first tab
this.onTabChanged(firstTabNameTag);
th.setOnTabChangedListener(this);
}
addTab()
方法非常简单,它会膨胀对象并在需要时将其分离。
private static void addTab(final MyClass activity, final TabHost tabHost, final TabHost.TabSpec tabSpec, final TabInfo tabInfo) {
tabSpec.setContent(new TabFactory(activity)); // This is just an empty View
final String tag = tabSpec.getTag();
// Here I check if there's already a Fragment for that tab, probably in a
// previously saved state. If this happens, we deactivate it, because our
// former state for that tab is "not shown".
tabInfo.setFragment(activity.getSupportFragmentManager().findFragmentByTag(tag));
if ((tabInfo.getFragment() != null) && (!tabInfo.getFragment().isDetached())) {
final FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
ft.detach(tabInfo.getFragment());
ft.commit();
activity.getSupportFragmentManager().executePendingTransactions();
}
// Actually, there's where I call the "official" `addTab()` from `TabHost`
tabHost.addTab(tabSpec);
}
所以只剩下TabFragmentInflater了。它只是一个Fragment
根据我上面提到的唯一ID扩展相应的布局,所以它是这样的:
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
fragId = getArguments().getInt("fragid");
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
switch (fragId) {
case 1: // My first tab...
final LinearLayout fragLayout = (LinearLayout) inflater.inflate(R.layout.myfirsttab_fragment_layout, container, false);
...
return fragLayout;
case 2: // My second tab
fragLayout = (LinearLayout) inflater.inflate(R.layout.mysecondtab_fragment_layout, container, false);
...
return fragLayout;
...
}
}
catch (final InflateException e) { return view; }
return null;
}