Pai-Hsiang Huang
Pai-Hsiang Huang

Reputation: 372

It is possible to replace fragments with the same tag name?

I have a container to frame fragments on the screen. I use the following code to replace fragment:

getSupportFragmentManager()
    .replace(R.id.container, MyFragment.newInstance(), "same_fragment_tag")
    .addToBackStack(null)
    .commit();

If I call it a couple of times, and after, trying to find the fragment with:

Fragment target = getSupportFragmentManager().findFragmentByTag("same_fragment_tag");

Which instance of the fragment would I get?

Upvotes: 1

Views: 845

Answers (1)

Anes Hasic
Anes Hasic

Reputation: 424

It will look for the last fragment that has been added to the fragment manager.

If it's unable to find it then it will look at all fragments that still are active and haven't been removed.

Upvotes: 1

Related Questions