Sandhiya
Sandhiya

Reputation: 359

IllegalStateException while begin transaction for Fragment

After getting success or failure response from Retrofit, I am displaying a alert dialog. Alert dialog class is extending DialogFragment.

Before displaying dialog if I click home button, then my app crashes.

I have searched and tried below but that doesn't helped me.

 @Override
    protected void onPostResume() {
        super.onPostResume();
        isTransactionSafe = true;
        if(isTransactionPending)
            showDialog(alertDialog);
    }


    @Override
    protected void onPause() {
        super.onPause();
        isTransactionSafe = true;
    }



    public void showDialog(UpdateAlertDialog updateAlertDialog) {


        // DialogFragment.show() will take care of adding the fragment
        // in a transaction.  We also want to remove any currently showing
        // dialog, so make our own transaction and take care of that here.
        alertDialog=updateAlertDialog;
        if (isTransactionSafe) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
            if (prev != null) {
                ft.remove(prev);
            }
            ft.addToBackStack(null);
            // Create and show the dialog.
            updateAlertDialog.show(ft, "dialog");
            isTransactionPending=false;
        }
        else{
            isTransactionPending=false;
        }
    }
}

Here is my stack trace,

  Process: com.nexge.zalcon, PID: 17629
    java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        at androidx.fragment.app.FragmentManagerImpl.checkStateLoss(FragmentManagerImpl.java:1536)
        at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManagerImpl.java:1558)
        at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:317)
        at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:282)
        at androidx.fragment.app.DialogFragment.show(DialogFragment.java:172)
        at com.nexge.zalcon.activity.DetailsRecyclerView.showDialog(DetailsRecyclerView.java:400)
        at com.nexge.zalcon.activity.DetailsRecyclerView$2.onResponse(DetailsRecyclerView.java:349)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6651)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

Anybody help me to solve this...

Upvotes: 1

Views: 142

Answers (2)

Bracadabra
Bracadabra

Reputation: 3659

There is wrong assignment in onPause, just set isTransactionSafe to false:

@Override
protected void onPause() {
    super.onPause();
    isTransactionSafe = false;
}

Upvotes: 1

rmdan
rmdan

Reputation: 123

i think you should check

if (getActivity() != null && isAdded())

before displaying the dialog

Upvotes: 0

Related Questions