撤销权限正在杀死您未销毁的活动,当您返回时,它们可能会被恢复savedInstanceState
,而不是您正在测试的通常方式。
您发布的异常意味着您的代码在之后执行commit()
(或popBackStack()
)(或,通常在之后调用),或者由于某些异步操作,您在最小化时忘记取消。FragmentTransaction
onSaveInstanceState()
onPause()
onStop()
Activity
为了安全起见,我通常会跟踪我是否可以提交FragmentTransactions
,就像这样
public abstract class BaseActivity extends AppCompatActivity {
private boolean mFragmentTransactionsAllowed;
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFragmentTransactionsAllowed = true;
}
@Override
protected void onStart() {
super.onStart();
mFragmentTransactionsAllowed = true;
}
@Override
protected void onResume() {
super.onResume();
mFragmentTransactionsAllowed = true;
}
@Override
protected void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
mFragmentTransactionsAllowed = false;
}
protected final boolean areFragmentTransactionsAllowed() {
return mFragmentTransactionsAllowed;
}
}
在提交之前我会检查
if (areFragmentTransactionsAllowed()) {
ft.commit();
}