Fragment add or replace not working

The problem here is that you’re mixing android.support.v4.app.Fragment and android.app.Fragment. You need to convert all uses to use the support library, which also means calling getSupportFragmentManager().

Something like this, for example:

    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ExampleFragments fragment = new ExampleFragments();
    fragmentTransaction.replace(R.id.frag, fragment);
    fragmentTransaction.commit();

It is important to note that the support library Fragment and the normal Fragment are NOT interchangeable. They achieve the same purpose, but they cannot be replaced with one another in code.

Leave a Comment