从翻新获得成功或失败响应后,我将显示一个警报对话框。警报对话框类正在扩展DialogFragment。
在显示对话框之前,如果单击“主页”按钮,则我的应用程序崩溃。
我已经在下面进行搜索和尝试,但这并没有帮助我。
@Override
protected void onPostResume() {
super.onPostResume();
isTransactionSafe = true;
if(isTransactionPending)
showDialog(alertDialog);
}
@Override
protected void onPause() {
super.onPause();
isTransactionSafe = true;
}
public void showDialog(UpdateAlertDialog updateAlertDialog) {
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
alertDialog=updateAlertDialog;
if (isTransactionSafe) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
updateAlertDialog.show(ft, "dialog");
isTransactionPending=false;
}
else{
isTransactionPending=false;
}
}
}
这是我的堆栈跟踪,
Process: com.nexge.zalcon, PID: 17629
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at androidx.fragment.app.FragmentManagerImpl.checkStateLoss(FragmentManagerImpl.java:1536)
at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManagerImpl.java:1558)
at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:317)
at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:282)
at androidx.fragment.app.DialogFragment.show(DialogFragment.java:172)
at com.nexge.zalcon.activity.DetailsRecyclerView.showDialog(DetailsRecyclerView.java:400)
at com.nexge.zalcon.activity.DetailsRecyclerView$2.onResponse(DetailsRecyclerView.java:349)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
有人帮我解决这个问题...
最佳答案
onPause中的分配有误,只需将isTransactionSafe设置为false:
@Override
protected void onPause() {
super.onPause();
isTransactionSafe = false;
}
关于android - 在开始Fragment事务时发生IllegalStateException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59051516/