我有一个带有 ABS 和导航类型选项卡的应用程序。在其中一个选项卡中,我有 3 个片段(不同排列的纵向/横向):一个有列表,一个有封面流,最后一个有一些搜索功能。当按下列表项或覆盖流项或搜索结果时,我打开应该作为片段全屏加载的详细信息片段。另一个问题是,对于每个片段,我必须调用一个 web 服务来返回要显示的信息。但是我不能并行调用它们,所以我首先加载列表,然后是覆盖流,然后是搜索选项。否则它会给出一个服务器错误,例如:“连接仍然分配”。
问题:更改配置后将无法按预期工作:因为我不想根据新配置加载所需布局的详细信息,也不想加载后台堆栈中的三个,并且以某种方式没有服务器错误:D
我试过的: 1)活动没有 configChanges 属性,所以活动每次都重新启动。这适用于三个片段,但是当我打开一个细节时,它给了我我谈到的服务器错误。因为它试图同时从 backstack 中恢复所有 3 个片段以及详细片段。
查看 logcat 输出:
06-12 12:31:38.511: E/EvenementsMainActivity(23833): onCreate
06-12 12:31:38.551:E/NouveauLieuxFragment(23833):onCreateView
06-12 12:31:38.561: E/Top5Fragment(23833): onCreateView
06-12 12:31:38.571:E/RechercheFragment(23833):onCreateView
06-12 12:31:38.611: E/SalleDetailsFragment(23833): onCreateView
06-12 12:31:38.641:W/PhoneWindow(23833):以前聚焦的视图在保存过程中报告了 id 2131099775,但在恢复过程中找不到。
06-12 12:31:38.641: I/EvenementsMainActitity(23833): onResumeFragments
06-12 12:31:38.671: I/EvenementsMainActitity(23833): onTabSelected - Les Tops
06-12 12:31:38.671: E/NouveauLieuxFragment(23833): onCreateView
06-12 12:31:38.681:W/SingleClientConnManager(23833):SingleClientConnManager 的使用无效:连接仍然分配。
06-12 12:31:38.681:W/SingleClientConnManager(23833):确保在分配另一个连接之前释放连接。
06-12 12:31:38.721: E/Top5Fragment(23833): onCreateView 06-12 12:31:38.731: E/RechercheFragment(23833): onCreateView
06-12 12:31:38.831:I/coverflow(23833):中心coverflow 427
06-12 12:31:38.851: E/SalleDetailsFragment(23833): GetSalleById 错误服务器错误
06-12 12:31:38.851: W/System.err(23833): com.neurospeech.wsclient.SoapFaultException: 服务器错误
- 其中 EvenementsMainActitity 是主要活动;NouveauLieuxfragment、Top5Fragment 和 RechercheFragment 是我说的 3 个片段,而 SalleDetailsFragment 是细节片段
- 我似乎无法理解为什么
oncreateview
片段被调用两次:(
- 我似乎无法理解为什么
2) 使用活动的 configChanges 属性,所以我编写了 onConfigurationChanged 方法,但就像我所做的那样,它只是恢复了我来自的选项卡,即使我很详细。
3)现在我正在尝试将细节更改为活动,但这非常困难,因为我需要一堆变量,我必须通过捆绑发送它们,而且我不会有操作栏等......
注意:我必须再次显示配置中的活动和片段,原因是方向的不同布局。
让我知道我有更多信息或代码可以提供更好的理解。
编辑:我有一个包含选项卡的 SherlockFragmentActivity,每个选项卡都有一个 SherlockFragment,细节相同。比我有:
private class MyTabListener implements ActionBar.TabListener {
private final Activity mActivity;
public MyTabListener(Activity activity, String tag) {
mActivity = activity;
}
@Override
public void onTabSelected(Tab tab, final FragmentTransaction ft) {
....
tab_position = tab.getPosition();
switch (tab_position) {
...
case 1: // Les Tops
if (BuildMode.DEBUG) {
Log.i(TAG, "onTabSelected - Les Tops");
}
preInitializedFragment1 = (SherlockFragment) ((SherlockFragmentActivity) mActivity).getSupportFragmentManager()
.findFragmentByTag(fragments_mTags.get(2));
preInitializedFragment2 = (SherlockFragment) ((SherlockFragmentActivity) mActivity).getSupportFragmentManager()
.findFragmentByTag(fragments_mTags.get(10));
if (!isPhone)
preInitializedFragment3 = (SherlockFragment) ((SherlockFragmentActivity) mActivity).getSupportFragmentManager()
.findFragmentByTag(fragments_mTags.get(8));
if (preInitializedFragment1 == null && preInitializedFragment2 == null
&& ((!isPhone && preInitializedFragment3 == null) || isPhone)) {
frag1 = (SherlockFragment) SherlockFragment.instantiate(mActivity, NouveauLieuxFragment.class.getName());
ft.add(R.id.content1, frag1, fragments_mTags.get(2));
frag2 = (SherlockFragment) SherlockFragment.instantiate(mActivity, Top5Fragment.class.getName());
ft.add(R.id.content2, frag2, fragments_mTags.get(10));
if (!isPhone) {
frag3 = (SherlockFragment) SherlockFragment.instantiate(mActivity, RechercheFragment.class.getName());
ft.add(R.id.content3, frag3, fragments_mTags.get(8));
}
} else if (frag1 != null && frag2 != null && ((!isPhone && frag3 != null) || isPhone)) {
ft.attach(frag1);
ft.attach(frag2);
if (!isPhone)
ft.attach(frag3);
} else if (preInitializedFragment1 != null && preInitializedFragment2 != null
&& ((!isPhone && preInitializedFragment3 != null) || isPhone)) {
ft.attach(preInitializedFragment1);
ft.attach(preInitializedFragment2);
frag1 = preInitializedFragment1;
frag2 = preInitializedFragment2;
if (!isPhone) {
ft.attach(preInitializedFragment3);
frag3 = preInitializedFragment3;
}
}
loadLesTops();
break;
....
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
....
// Detach the fragment, because another one is being attached
if (frag1 != null) {
ft.detach(frag1);
}
if (frag2 != null) {
ft.detach(frag2);
}
if (frag3 != null) {
ft.detach(frag3);
}
}
}