onActivityResult中的操作和“错误无法在onSaveInstanceState之后执行此操作”

实现用户可以login的应用程序我有以下情况:如果用户login执行操作,否则请启动结果的login活动,如果结果是Activity.RESULT_OK,请执行操作。

我的问题是,perfom的动作是显示一个DialogFragment,但调用

DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel); newFragment.show(ft, "dialog") 

在onActivityResultcallback中抛出一个exception:

 Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 

那我该如何解决呢? 我正在考虑在那里举起一面旗帜,并在onResume中显示对话框,但我看到这个解决scheme有点肮脏

编辑:添加更多的代码(我按照这个例子显示DialogFragment

当用户请求动作时:

 ... if (!user.isLogged()){ startActivityForResult(new Intent(cnt, Login.class), REQUEST_LOGIN_FOR_COMMENT); } 

在同一个片段

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_LOGIN_FOR_COMMENT && resultCode == Activity.RESULT_OK) { FragmentTransaction ft = getFragmentManager().beginTransaction(); DialogFragment newFragment = MyDialogFragment.newInstance(); newFragment.show(ft, "dialog") } } 

如果用户loginlogin活动调用;

 setResult(Activity.RESULT_OK); finish(); 

我提出的最好的事情是不使用.show(),而是做到这一点。

 CheckinSuccessDialog dialog = new CheckinSuccessDialog(); //dialog.show(getSupportFragmentManager(), null); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.add(dialog, null); ft.commitAllowingStateLoss(); 

这是对我来说工作正常的解决方法。

 private void alert(final String message) { Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { public void run() { AlertDialogFragment alertDialogFragment = AlertDialogFragment.newInstance(message); alertDialogFragment.show(getFragmentManager(), ALERT_DIALOG_FRAGMENT); } }); } 

如果使用DialogFragment,唯一能解决的就是用dissmissAllowingStateLoss()

我只是检查一个活动是否被破坏。

 if (!getActivity().isFinishing()) { DialogFragment fragment = MyFragment.newInstance(); fragment.show(getActivity().getSupportFragmentManager(), MyFragment.TAG); } 

重写show(Fragment manager, String tag)function,允许状态丢失并改变mDismissed = false; mShownByMe = true; 从原始function反思:

 public class DialogParent extends DialogFragment { @Override public void show(FragmentManager manager, String tag) { try { Field mDismissed = DialogFragment.class.getDeclaredField("mDismissed"); Field mShownByMe = DialogFragment.class.getDeclaredField("mShownByMe"); mDismissed.setAccessible(true); mShownByMe.setAccessible(true); mDismissed.setBoolean(this, false); mShownByMe.setBoolean(this, true); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } FragmentTransaction ft = manager.beginTransaction(); ft.add(this, tag); ft.commitAllowingStateLoss(); } } 

这真实的作品。

 CheckinSuccessDialog dialog = new CheckinSuccessDialog(); //dialog.show(getSupportFragmentManager(), null); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.add(dialog, null); ft.commitAllowingStateLoss(); 

但是,但仍然不好,因为得到错误“活动已被破坏”

 ava.lang.IllegalStateException: Activity has been destroyed fragmentTransaction.commitAllowingStateLoss(); 

所以我的解决scheme是添加检查, if (!isFinishing()&&!isDestroyed())

 CheckinSuccessDialog fragment = CheckinSuccessDialog.newInstance(); if (fragment instanceof DialogFragment) { DialogFragment dialog = (DialogFragment) fragment; if (!dialog.isAdded()) { fragmentTransaction.add(dialog, CheckinSuccessDialog.class.getName()); if (!isFinishing()&&!isDestroyed()) { fragmentTransaction.commitAllowingStateLoss(); } } 

closures:

 FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); Fragment fragment = getSupportFragmentManager().findFragmentByTag(CheckinSuccessDialog.class.getName()); if (fragment != null && fragment instanceof DialogFragment) { DialogFragment dialog = (DialogFragment) fragment; dialog.dismiss(); if (!isFinishing()&&!isDestroyed()) { fragmentTransaction.commitAllowingStateLoss(); } }