问题:
我以编程方式将一个片段添加到LinearLayout。它出现在我的活动中,很棒。 我转动设备 - >配置更改:一切都被销毁以便重新创建。但是,在调用onDestroy()之前,应该调用onSaveInstanceState()。这是父活动的情况,但不是我添加的片段。为什么?
代码:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/parent_LL"
android:stuff="myStuff"
>
<LinearLayout
android:id="@id/categories_activity_LL1"
android:stuff="myStuff" />
<LinearLayout
android:id="@id/categories_activity_LL2"
android:stuff="myStuff" />
</LinearLayout>
我将片段添加到父活动的UI中:
ft.add(container1, categories, CatFragIds.CATEGORIES.toString()).commit();
我覆盖片段的onSaveInstanceState():
@Override
public void onSaveInstanceState(Bundle outState) {
// Récupère extensible
boolean extensible = ((CategoryAppsListView) this.getListView())
.isExtensible();
mState.setExtensible(extensible);
// Transmet l'état de CategoriesListElems
FragmentManager fm = getFragmentManager();
@SuppressWarnings("unchecked")
FragRetainObject<CategoriesListElemsState> retainedState =
(FragRetainObject<CategoriesListElemsState>)
fm.findFragmentByTag(CATEGORIESLISTELEMS_STATE+"_"+this.getTag());
if( retainedState == null) {
retainedState =
FragRetainObject.<CategoriesListElemsState>newInstance(mState);
fm.beginTransaction()
.add(retainedState, CATEGORIESLISTELEMS_STATE+"_"+this.getTag()).commit();
}
else retainedState.setRetainObj(mState);
super.onSaveInstanceState(outState);
}
感谢您的时间! : - )
答案 0 :(得分:1)
我遇到了同样的问题。有两种可能的答案:
答案 1 :(得分:-2)
您需要将配置更改作为方向和键盘放在AndroidManifest.xml
中