Java.lang.IllegalStateException : commit already called error … here is a solution to the problem.
Java.lang.IllegalStateException : commit already called error
I’ve tried the code below
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)
Solution
newFragment.show(ft, "dialog")
Displays the call internal commit. So you might want to get rid of
ft.commitAllowingStateLoss();
Or you can remove it
newFragment.show(ft, "dialog");
Add a 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;
}