我有一个 DialogFragment 我尝试在我的 Activity 的覆盖 onPostResume() 方法中创建,但我收到此错误消息:java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
我已经阅读了类似问题的所有答案(此处、此处、此处和此处的博客),但没有一个对我有用(onPostResume 修复或 Runnable 修复),或者不适合 - 我不想使用 commitAllowingStateLoss 因为它不安全,而且我不想在不调用 super() 的情况下覆盖 onSaveInstanceState。
编辑:我已将问题缩小到我对 Eventbus 的使用。当我直接在 onPostResume 方法中创建对话框片段时,我没有收到任何错误。当我调用 eventbus 事件来创建对话框片段时,我得到了上述错误。我尝试将事件处理程序的线程更改为:
public void onEvent(DisplayOperatorDialogEvent event)
public void onEventMainThread(DisplayOperatorDialogEvent event)
但这似乎没有什么不同。
编码:
@Override
protected void onPostResume()
{
super.onPostResume();
Log.d("MainActivity","in onPostResume");
Intent receivedIntent = getIntent();
String receivedAction = receivedIntent.getAction();//null if started from the menu
try
{
String receivedText;
if(receivedAction!=null && receivedAction.equals("display_dialog"))
{
receivedText = receivedIntent.getStringExtra("dialog");
if(receivedText!=null && receivedText.equals("operator"))
{
String title = "hello";
String msg = "test";
DisplayOperatorDialogEvent dialogEvent = new DisplayOperatorDialogEvent(title, msg);
EventBus.getDefault().post(dialogEvent);
}
}
receivedIntent.setAction("");
receivedIntent.removeExtra("screen");
}
catch(Exception e)
{
Log.e("Error: parse the notification screen intent: ", "" + e);
}
}
public void onEvent(DisplayOperatorDialogEvent event)
{
{
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
android.support.v4.app.Fragment prev = getSupportFragmentManager().findFragmentByTag("operator");
if (prev != null)
{
ft.remove(prev);
}
ft.addToBackStack(null);
operatorDialog = new OperatorDialogFragment();
Bundle args = new Bundle();
args.putString("title", event.getTitle());
args.putString("message", event.getMsg());
operatorDialog.show(ft,"operator");
}
}
追溯:
10-05 19:06:43.115 20803-20803/com.unifiapp E/Event﹕ Could not dispatch event: class com.unifiapp.events.Events$DisplayOperatorDialogEvent to subscribing class class com.unifiapp.MainActivity
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:155)
at com.unifiapp.MainActivity.onEvent(MainActivity.java:478)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at de.greenrobot.event.EventBus.invokeSubscriber(EventBus.java:569)
at de.greenrobot.event.EventBus.postToSubscription(EventBus.java:500)
at de.greenrobot.event.EventBus.postSingleEvent(EventBus.java:475)
at de.greenrobot.event.EventBus.post(EventBus.java:365)
at com.unifiapp.MainActivity.onPostResume(MainActivity.java:676)
at android.app.Activity.performResume(Activity.java:5323)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Android 4.0 和 4.4 都会出现此问题。