我有两个可序列化的对象,我想在重建片段后保留这些对象。因此我在onSaveInstanceState期间保存了它们(这里的对象不为null):
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(RENDERER, renderer);
outState.putSerializable(SERIES, series);
}
在片段的onCreate方法中,我尝试将它们从Bundle中取出:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
renderer = (DefaultRenderer) savedInstanceState.getSerializable(RENDERER);
series = (CategorySeries) savedInstanceState.getSerializable(SERIES);
}
}
问题是“renderer”和“series”在调用getSerializable后始终为null。有什么想法吗?
答案 0 :(得分:3)
您是否覆盖托管此片段的onSaveInstanceState
中的FragmentActivity
方法?如果您是,请确保它应调用super.onSaveInstanceState(Bundle)
。