John Garcia
John Garcia

Reputation: 11

How to replace fragment that is loaded by navigationView

I have a navigation view that loads some fragments depending on which is pressed. In one of the fragments, I want to have a button, which when it is pressed, the fragment changes from the one loaded by the navigation view to a different fragment. I have tried using

ProfileEditFragment pef = new ProfileEditFragment();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().remove(pef).replace(R.id.drawer_layout, pef, "Fragment")
                .addToBackStack(null).commit();

When I do this, it loads the other fragment on top of the current fragment, rather than replace it, even though I used the replace function. How do I fix this?

Upvotes: 1

Views: 98

Answers (1)

Mehmet Gür
Mehmet Gür

Reputation: 522

Can you try this:

ProfileEditFragment pef = new ProfileEditFragment();
FragmentTransaction transaction =getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.drawer_layout, pef);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

Upvotes: 1

Related Questions