R. Viswanathan
R. Viswanathan

Reputation: 1

Replacing specific fragment by using fragment transaction

In Android, I have added several fragments in Linear Layout by using Fragment Transaction. Is there any specific way by which a specific fragment in the Linear Layout can be replaced and not all of them?

We can remove any fragment by using the Fragment itself, but we can only replace a specific fragment by the container ID and not the Fragment itself?

fragmentTransaction.replace(R.id.linearlayout, fragment); replaces the entire linear layout by one single fragment and not a specific one that I am trying to replace with.

Replacing a fragment in a container by tag also has no effect from the above statement.

fragmentTransaction.addToBackStack(null); does not remove the specific fragment that I am trying to replace with.

So is also the case when I tried to replace R.id.linearlayout with the root view id of the Fragment layout file.

I tried to add all fragments in linear layout at beginning and detach fragments which are not to be displayed in the beginning. I can detach unneeded fragments and attach needed fragments during run time, but newly attached fragments just get added at the end of the linear layout, but not at the specific position that I am trying to replace with.

Is there any way to attach new fragments in Linear Layout at a specific position? It is possible to detach all existing fragments from Linear Layout and attach all needed fragments at run time for just replacing one specific fragment at a given position, but this takes as much as O(n) time.

Is it possible to do that better, in just O(1) time?

Upvotes: 0

Views: 138

Answers (0)

Related Questions