Removing fragments from an activity

In my personal case, I add and remove fragment this way:

 if(getSupportFragmentManager().findFragmentById(R.id.container) != null) {
        getSupportFragmentManager()
                .beginTransaction().
                remove(getSupportFragmentManager().findFragmentById(R.id.container)).commit();
    }
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.container, fragment)
            .commit();

getSupportFragmentManager().beginTransaction().
remove(getSupportFragmentManager().findFragmentById(R.id.frame)).commit();

Try this, it should work.

public void switchContent(Fragment fragment) {
    Fragment oldFragment = getSupportFragmentManager().findFragmentById(R.id.frame);
    if (oldFragment != null) { 
        getSupportFragmentManager().beginTransaction()
            .remove(oldFragment).commit();
    }
    mContent = fragment;
    getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.content_frame, fragment)
        .commit();
    getSlidingMenu().showContent();
}

Tags:

Android