Simon
Simon

Reputation: 1731

Commit already called error, while changing fragment

I recently stumbled into the error "Can not perform this action after onSaveInstanceState" while giving permission (Android M) to access external storage. I tackled and solved the problem according this post (I thought).

Now I am running into a new problem, as I get and error "commit already called" while trying to change a fragment.

As soon as I wanna switch to a certain Fragment (Discussionfragment as example) I get the error "commit already called", see logcat.

I already searched for some solutions on stackoverflow but I don`t seem to see what is wrong, I know there is a commit to much but how to handle it in my situation? Can anybody help me please?

Call in Discussionfragment:

public class DiscussionsActivity extends BaseActivity
{
..
  discussionsFragment = DiscussionsFragment.getInstance(id);
  changeContent(discussionsFragment, R.id.content, false); <---
..
}

BaseActivity:

public class BaseActivity extends AppCompatActivity {

    protected Fragment currentFragment;
    protected MainFragment mainFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private boolean mReturningWithResult = false;

    public void changeContent(Fragment fragment, int content, boolean inBackStack) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        try {
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        } catch (Exception e) {
        }
        fragmentManager = getSupportFragmentManager();
        String newTag = ((Object) fragment).getClass().getName() + ":" + fragmentManager.getBackStackEntryCount();

        transaction = fragmentManager.beginTransaction().replace(content, fragment, newTag);

        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        if (inBackStack) {
            transaction.addToBackStack(fragment.getClass().getSimpleName());
        } else
            mReturningWithResult = true; <--- handle onSaveInstanceState error in onResume
            try {
                fragmentManager.popBackStackImmediate();
                transaction.commit();
            } catch (IllegalStateException ignored) {
                // There's no way to avoid getting this if saveInstanceState has already been called.
            }

        currentFragment = fragment;
    }

@Override
    public void onResume() {
        super.onResume();
        if (mReturningWithResult) {
            fragmentManager.popBackStackImmediate();

            if (currentFragment != mainFragment) {
                transaction.commit(); <---- HERE THE CRASH HAPPENS
            }
        }
        // Reset the boolean flag back to false for next time.
        mReturningWithResult = false;
    }

Logcat:

FATAL EXCEPTION: main
Process: some.com.app, PID: 17374
java.lang.RuntimeException: Unable to resume activity {some.com.app/some.com.app.UI.Discussion.DiscussionsActivity_}: java.lang.IllegalStateException: commit already called
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: commit already called
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:662)
at some.com.app.UI.Base.BaseActivity.onResume(BaseActivity.java:144) <--

Upvotes: 1

Views: 1923

Answers (2)

QuokMoon
QuokMoon

Reputation: 4425

You may trying commit twice with same fragment transaction after commit statement . Create a new fragment transaction if previous transaction already committed.

Also recommend for ensure commit() use debug point.

public void changeContent(Fragment fragment, int content, boolean inBackStack) {
 ...........
 ...........
     try {
         fragmentManager.popBackStackImmediate();
         transaction.commit();
       } catch (IllegalStateException ignored) {
       // There's no way to avoid getting this if saveInstanceState has already been called.

 ...........
 ...........


      }


@Override
 public void onResume() {
     super.onResume();
      ...........
      ...........
       //This method would be called at second time.

       transaction.commit(); <---- HERE THE CRASH HAPPENS

        // Reset the boolean flag back to false for next time.
        mReturningWithResult = false;
      ...........
      ...........
    }

Upvotes: 1

Nas
Nas

Reputation: 2198

remove global FragmentTransaction and declare it locally

public void changeContent(Fragment fragment, int content, boolean inBackStack) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        try {
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        } catch (Exception e) {
        }
        fragmentManager = getSupportFragmentManager();
        String newTag = ((Object) fragment).getClass().getName() + ":" + fragmentManager.getBackStackEntryCount();

        FragmentTransaction  transaction = fragmentManager.beginTransaction().replace(content, fragment, newTag);

        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        if (inBackStack) {
            transaction.addToBackStack(fragment.getClass().getSimpleName());
        } else
            mReturningWithResult = true; <--- handle onSaveInstanceState error in onResume
            try {
                transaction.commit();
            } catch (IllegalStateException ignored) {
                // There's no way to avoid getting this if saveInstanceState has already been called.
            }

        currentFragment = fragment;
    }

Upvotes: 1

Related Questions