3

I have tried the following code,

try {
    final Activity activity = ctx;
    FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
    android.app.Fragment prev = activity.getFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    DialogServiceFailed newFragment = DialogServiceFailed.newInstance(pageName, onServiceFailed);
    newFragment.show(ft, "dialog");
    ft.addToBackStack(null);
    ft.commitAllowingStateLoss();
} catch (ClassCastException e) {
    Log.d("Log", "Can't get the fragment manager with this");
}

But I get the following exception and my app crashes.

java.lang.IllegalStateException: commit already called
        at android.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
        at android.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
        at com.brightspot.extrain5psim.model.APIRequestHandler.loadServiceFailedDialog(APIRequestHandler.java:99)
        at com.brightspot.extrain5psim.view.fragments.LoginFragment.setOnAsyncTaskCompleted(LoginFragment.java:201)
        at com.brightspot.extrain5psim.model.APIRequestHandler.onPostExecute(APIRequestHandler.java:80)
        at com.brightspot.extrain5psim.model.APIRequestHandler.onPostExecute(APIRequestHandler.java:17)
        at android.os.AsyncTask.finish(AsyncTask.java:631)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5317)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128

1 Answers1

7
newFragment.show(ft, "dialog")

show calls internally commit. So you probably want get rid of

ft.commitAllowingStateLoss();

or you can get rid of

newFragment.show(ft, "dialog");

add call

ft.add(newFragment, "dialog");
ft.commitAllowingStateLoss();

Edit

this is what DialogFragment's show() looks like

 public int show(FragmentTransaction transaction, String tag) {
        mDismissed = false;
        mShownByMe = true;
        transaction.add(this, tag);
        mViewDestroyed = false;
        mBackStackId = transaction.commit();
        return mBackStackId;
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • At some point I got [this](http://stackoverflow.com/questions/7575921/illegalstateexception-can-not-perform-this-action-after-onsaveinstancestate-h/10261449#10261449) exception bro. That's why I added this line. If I removed `ft.commitAllowingStateLoss();` line then how can I avoid this exception? – Gunaseelan Jun 26 '15 at 13:16
  • If I get rid on `show` then how can I show the dialog bro. Sorry, if this is silly question. – Gunaseelan Jun 26 '15 at 13:19
  • `commitAllowingStateLoss` should take care of showing your DialogFragment – Blackbelt Jun 26 '15 at 13:20