Okan Okan - 20 days ago 7
Android Question

Fragment remove causes crash

I am showing settings fragment when user presses the settings button.And I want to remove it when user clicks back button from settings.

I am running following code when user clicks the settings button:

FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
SettingsFragment conv=new SettingsFragment();
SettingsFragment.newInstance(LOGGED_USERNAME,LOGIN_KEY);
fragmentTransaction.add(R.id.container,conv,"SettingsFragment");
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();


When user presses the back button from settings fragment I am running this:

Fragment fragment = Ffmanager.findFragmentByTag("SettingsFragment");
if(fragment != null) {
FfTransaction.remove(fragment).commit();
}


Scenario:

1)User on the main screen

2)Clicks settings button

3)Clicks back button from settings (Everything is good now)

4)Clicks again settings button

5)Then clicks again back button

Then app is crashed.But why ?

Logcat:

01-09 13:47:39.572: E/AndroidRuntime(2628): FATAL EXCEPTION: main
01-09 13:47:39.572: E/AndroidRuntime(2628): java.lang.IllegalStateException: commit already called
01-09 13:47:39.572: E/AndroidRuntime(2628): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
01-09 13:47:39.572: E/AndroidRuntime(2628): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
01-09 13:47:39.572: E/AndroidRuntime(2628): at com.impact.app.MainActivity.fragmentCleaner(MainActivity.java:422)
01-09 13:47:39.572: E/AndroidRuntime(2628): at com.impact.app.MainActivity.returnFromChat(MainActivity.java:1279)
01-09 13:47:39.572: E/AndroidRuntime(2628): at com.impact.app.MainActivity.overrideHome(MainActivity.java:1154)
01-09 13:47:39.572: E/AndroidRuntime(2628): at com.impact.app.MainActivity.onOptionsItemSelected(MainActivity.java:1097)
01-09 13:47:39.572: E/AndroidRuntime(2628): at android.app.Activity.onMenuItemSelected(Activity.java:2566)
01-09 13:47:39.572: E/AndroidRuntime(2628): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:373)
01-09 13:47:39.572: E/AndroidRuntime(2628): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:171)
01-09 13:47:39.572: E/AndroidRuntime(2628): at android.view.View.performClick(View.java:4240)
01-09 13:47:39.572: E/AndroidRuntime(2628): at android.view.View$PerformClick.run(View.java:17721)
01-09 13:47:39.572: E/AndroidRuntime(2628): at android.os.Handler.handleCallback(Handler.java:730)
01-09 13:47:39.572: E/AndroidRuntime(2628): at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 13:47:39.572: E/AndroidRuntime(2628): at android.os.Looper.loop(Looper.java:137)
01-09 13:47:39.572: E/AndroidRuntime(2628): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-09 13:47:39.572: E/AndroidRuntime(2628): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 13:47:39.572: E/AndroidRuntime(2628): at java.lang.reflect.Method.invoke(Method.java:525)
01-09 13:47:39.572: E/AndroidRuntime(2628): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-09 13:47:39.572: E/AndroidRuntime(2628): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-09 13:47:39.572: E/AndroidRuntime(2628): at dalvik.system.NativeStart.main(Native Method)

Answer

Basically you are committing the same FragmentTransaction twice.

Consider the exception message :

  java.lang.IllegalStateException: commit already called

Also consider :

public abstract int commit ()

Added in API level 11 Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.

A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state. See commitAllowingStateLoss() for situations where it may be okay to lose the commit.

Returns Returns the identifier of this transaction's back stack entry, if addToBackStack(String) had been called. Otherwise, returns a negative number.