我在不同场景下遇到以下错误信息:
每次调用 finish() 来结束 Activity-B,控制会回到 Activity-A。同样的过程会重复进行。做了2-3次后,我得到了以下错误信息:java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState E/AndroidRuntime( 9008):,为了避免这种情况,我遵循 https://dev59.com/qms05IYBdhLWcg3wD90B#10261438,但是现在出现了Java.lang.IllegalStateException: Activity has been destroyed。请问我错过了什么吗?
- Activity已被销毁
- 无法在onSaveInstanceState后执行此操作
public void beginTransaction(ID id, Bundle bundle) {
Fragment fragment = getFragmentItem(id);
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
fragment.setArguments(bundle);
// Add the fragment to the 'fragment_container' FrameLayout
fragmentManager.beginTransaction()
.add(R.id.fragment_container, fragment).commitAllowingStateLoss();
}
每次调用 finish() 来结束 Activity-B,控制会回到 Activity-A。同样的过程会重复进行。做了2-3次后,我得到了以下错误信息:java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState E/AndroidRuntime( 9008):,为了避免这种情况,我遵循 https://dev59.com/qms05IYBdhLWcg3wD90B#10261438,但是现在出现了Java.lang.IllegalStateException: Activity has been destroyed。请问我错过了什么吗?
明白了 我找到了解决方案...在添加片段的事务中出现了错误。我使用了.add而不是使用.replace(因为我的要求不需要维护堆栈),并使用commitAllowTransaction进行提交。我也阅读了http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html,看起来我所做的可能会很昂贵,但对于普通片段事务,提供的解决方法非常好用。
commitAllowingStateLoss()
到commit()
。 - SimasFragmentTransaction.replace
而不是FragmentTransaction.add
。请尝试并让我知道是否有帮助。 - Manish Mulimani