我有一个扩展 FragmentActivity 的简单类。我使用 getLastCustomNonConfigurationInstance/onRetainCustomNonConfigurationInstance 来保存当前状态。
public class MyActivity extends FragmentActivity {
static class Saver {
// ...
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Saver saver = (Saver) getLastCustomNonConfigurationInstance();
if (saver != null) {
// ...
}
// ...
}
@Override
public Object onRetainCustomNonConfigurationInstance() {
Saver saver = new Saver();
// ...
return saver;
}
}
当我关闭此活动时,有时会遇到一个奇怪的异常,例如:
java.lang.ClassCastException: Saver cannot be cast to android.support.v4.app.FragmentActivity$NonConfigurationInstances
at android.support.v4.app.FragmentActivity.getLastCustomNonConfigurationInstance(FragmentActivity.java:576)
...
你怎么看这个问题?提前致谢!