1

So I'm doing this thing after user gets back to my activity from a payment service:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bundle metadata = getMetadata();

    if (metadata == null || requestCode != metadata.getInt(REQ_CODE)) {
        return;
    }

    if (resultCode == RESULT_OK) {
        EventBus.getDefault().post(new PaymentResult(data, true));
    } else {
        EventBus.getDefault().post(new PaymentResult(data, false));
    }
}

In the method subscribing to PaymentResult I'm showing a dialog fragment (or rather - I'm trying to show a dialog fragment):

@Subscribe(threadMode = ThreadMode.MAIN)
public void onPaymentResult(PaymentResult result) {
    String message;

    if (result.getStatus()) {
        message = "ok";
    } else {
        message = "baaaad";
    }

    DialogFragment newFragment = NotificationFragment.newInstance(message);
    newFragment.show(((AppCompatActivity)mContext).getSupportFragmentManager(), "notification");
}

But I'm hitting this exception. How to overcome this?

Marek M.
  • 3,799
  • 9
  • 43
  • 93

1 Answers1

2

you need to use commitAllowingStateLoss() to avoid that exception

show fragment by this

    FragmentManager fm = ((AppCompatActivity)mContext).getSupportFragmentManager();
    NotificationFragment newFragment = NotificationFragment.newInstance(message);

    fm.beginTransaction()
               .add(newFragment, "notification")
               .commitAllowingStateLoss();

    fm.executePendingTransactions();

when you add a DialogFragment without a container id, then it is shown as Dialog else it will be shown like a normal fragment if container id is provided.

executePendingTransactions() is there for synchronous fragment transaction which is optional.

Nishant Pardamwar
  • 1,450
  • 12
  • 16