我的应用在不活动后间歇性崩溃。所以我想我没有正确存储东西。我打开“不保留 Activity ”进行故障排除,但现在我的应用程序到处崩溃。
堆栈跟踪:https://gist.github.com/hanleyhansen/6d41fee54b1e129b7922 这是丢失的布局:https://gist.github.com/hanleyhansen/73ace0c99ae675023e0f
最佳答案
我认为您可能遇到了 Issue 19917 的症状.此错误存在于 3.2 及更高版本中,并且最近才得到修复 (4.2)。同样的修复程序还没有进入支持库。
查看 comment 28一个真正的修复:你会想要编辑你的支持库并重新编译。编辑 v4/java/android/support/v4/app/FragmentManager.java
Bundle saveFragmentBasicState(Fragment f) {
Bundle result = null;
if (mStateBundle == null) {
mStateBundle = new Bundle();
}
f.onSaveInstanceState(mStateBundle);
if (!mStateBundle.isEmpty()) {
result = mStateBundle;
mStateBundle = null;
}
if (f.mView != null) {
saveFragmentViewState(f);
}
if (f.mSavedViewState != null) {
if (result == null) {
result = new Bundle();
}
result.putSparseParcelableArray(
FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
}
if (!f.mUserVisibleHint) {
// Only add this if it's not the default value
// @@@ BUG, result may not have been created, can be null!
if (result == null) {
result = new Bundle();
}
result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
}
return result;
}
如果您觉得无法胜任这项任务并希望等待 Google 修复支持库 这是另一个解决方法 Comment 8对于修复,您可以将其应用于所有 fragment
@Override
public void onSaveInstanceState(Bundle outState) {
//first saving my state, so the bundle wont be empty.
//http://code.google.com/p/android/issues/detail?id=19917
outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
super.onSaveInstanceState(outState);
}
我还遇到了 transaction.commitAllowingStateLoss();
作为对此的“修复”而不是 transaction.commit();
我发现的一些其他相关背景信息和解决方法可以帮助我解决 fragment 问题(尤其是在嵌套它们时)
- TNR 对 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState with DialogFragment 的回答(体面的解决方法)
- 如何修复 Android fragment 上的“在 onSaveInstanceState 之后无法执行此操作”] 4 (信息量很大)
- Chris Jenkins 对 Do fragments really need an empty constructor? 的回答
关于java - "Don' t Keep Activities“打开时找不到 ID 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17416263/