Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing DialogFragments crashes ICS

After the Fragments API being released, I started porting all my deprecated dialogs into DialogFraments using the compatibility package. Everything was working well, until I notice that my dialogs are causing crashes to ICS only:

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)

My dialogs are shown on AsyncTask.onPostExecute() in order to show an http response to the user. After diving into the problem, I got the conclusion that this exception only happens when the Activity is paused or stopped, and it does not happen on other versions of Android. I've tried using commitAllowingStateLoss(), but it doesn't help, since the exception is thrown on DialogFragment.show(). Here's my code for the 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();
}

Is it a bug on ICS? What am I supposed to do?

like image 923
Flávio Faria Avatar asked Dec 15 '11 13:12

Flávio Faria


2 Answers

I encountered this problem and could find no way in the framework to address this issue.

However I did provide a workaround to the problem which you can see at the following link

like image 196
quickdraw mcgraw Avatar answered Nov 15 '22 05:11

quickdraw mcgraw


This Google link addresses the same issue. Looks like its a bug in compatibility lib.

like image 27
AlexIIP Avatar answered Nov 15 '22 07:11

AlexIIP