9

Fragments APIがリリースされた後、互換性パッケージを使用して、廃止予定のすべてのダイアログをDialogFramentsに移植し始めました。DialogFragmentsの表示ICS

E/AndroidRuntime( 883): FATAL EXCEPTION: main 
E/AndroidRuntime( 883): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
E/AndroidRuntime( 883): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1254) 
E/AndroidRuntime( 883): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1265) 
E/AndroidRuntime( 883): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541) 
E/AndroidRuntime( 883): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:525) 
E/AndroidRuntime( 883): at android.support.v4.app.DialogFragment.show(DialogFragment.java:123) 
E/AndroidRuntime( 883): at com.myapp.ui.dialogs.TwoButtonDialogFragment.showDialog(TwoButtonDialogFragment.java:84) 

私のダイアログがユーザーにHTTP応答を示すためにAsyncTask.onPostExecute()に表示されます:私は私のダイアログが唯一のICSにクラッシュを引き起こしていることに気づくまで、すべてが、うまく働いていました。この問題が発生した後、この例外はアクティビティが一時停止または停止したときにのみ発生し、Androidの他のバージョンでは発生しないという結論に達しました。 私はcommitAllowingStateLoss()を使用しようとしましたが、DialogFragment.show()で例外がスローされるため、役立たない。 DialogFragmentのコードは次のとおりです。

private static void showDialog(FragmentActivity activity, String title, String msg, 
     String positiveButtonText, String negativeButtonText, int id, Bundle args) { 

    if (activity.isFinishing()) { 
     return; 
    } 

    FragmentManager fmgr = activity.getSupportFragmentManager(); 
    FragmentTransaction ft = fmgr.beginTransaction(); 
    Fragment prev = fmgr.findFragmentByTag(TAG); 
    if (prev != null) { 
     try { 
      ft.remove(prev); 
     } catch (IllegalStateException ex) { 
      // issue: http://code.google.com/p/android/issues/detail?id=17029 
     } 
    } 

    TwoButtonDialogFragment newFragment = new TwoButtonDialogFragment(); 
    if (args == null) { 
     args = new Bundle(); 
    } 
    args.putString("title", title); 
    args.putString("message", msg); 
    args.putString("positiveButtonText", positiveButtonText); 
    args.putString("negativeButtonText", negativeButtonText); 
    args.putInt("id", id); 
    newFragment.setArguments(args); 
    newFragment.setCancelable(false); 
    newFragment.show(fmgr, TAG); // exception is thrown here 
    ft.commit(); 
} 


@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    final Bundle args = getArguments(); 
    String title = args.getString("title"); 
    String msg = args.getString("message"); 
    String positiveButtonText = args.getString("positiveButtonText"); 
    String negativeButtonText = args.getString("negativeButtonText"); 
    final int id = args.getInt("id"); 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    if (!TextUtils.isEmpty(title)) { 
     builder.setTitle(title); 
    } 
    builder.setMessage(msg); 

    final TwoButtonDialogHandler handler = (TwoButtonDialogHandler) getActivity(); 
    builder.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) {    
      handler.doPositiveClick(id, args); 
     } 
    }); 
    builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      handler.doNegativeClick(id, args); 
     } 
    }); 

    return builder.create(); 
} 

ICSのバグですか?私はどうしたらいいですか?

+0

類似の質問と回答については、[こちら](http://stackoverflow.com/questions/7992496/how-to-handle-asynctask-onpostexecute-when-paused-to-avoid-illegalstateexception)を参照してください。 – PJL

答えて

3

この問題が発生し、この問題に対処するためのフレームワークがない可能性があります。

は、しかし、私はあなたが以下の link

2

このGoogleのlinkアドレスと同じ問題に見ることができる問題の回避策を提供しました。互換性のlibのバグのように見えます。

関連する問題