Engima
Engima

Reputation: 49

How do I get the current Fragment and replace it with another fragment

I am working on a navigation drawer and am currently using:

getSupportFragmentManager().beginTransaction().replace(R.id.mainfragment, f1).commit();

but as you can see that always replaces the main fragment with the new one. Thanks!

Upvotes: 2

Views: 1537

Answers (2)

Nir Duan
Nir Duan

Reputation: 6392

final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(getId(), new SearchVideoDownloadFragment(), "NewFragmentTag");
ft.commit();

Upvotes: 1

geokavel
geokavel

Reputation: 619

You can do beginTransaction().replace (container, newFragment, tag), where container is the id of a ViewGroup in an xml file. So the thing being replaced is whichever fragment is currently in the container.

Upvotes: 1

Related Questions