I haven't found something like setTag(String tagName)
method in the Fragment
class. The only way to set a Fragment
tag that I have found is by doing a FragmentTransaction
and passing a tag name as parameter.
Is this the only way to explicitly set a Fragment
tag by code?
You can provide a tag inside your activity layout xml file.
"Supply the android:tag attribute with a unique string."
just as you assign id in a layout xml.
link to developer guide
Yes. So the only way is at transaction time, e.g. using
add
,replace
, or as part of the layout.I determined this through an examination of the compatibility sources as I briefly looked for similar at some point in the past.
I know it's been 6 years ago but if anyone is facing the same problem do like i've done:
You can set tag to fragment in this way:
You can add the tag as a property for the
Fragment
arguments. It will be automatically restored if the fragment is destroyed and then recreated by the OS.Example:-
This is the best way I have found :
}