问题:
我以编程方式向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>
我将片段添加到父活动的用户界面中:
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);
}
谢谢你的时间!!:-)