How to set a Fragment tag by code?

前端 未结 8 2375
我寻月下人不归
我寻月下人不归 2020-12-01 02:26

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 fo

8条回答
  •  执笔经年
    2020-12-01 02:51

    Nowadays there's a simpler way to achieve this if you are using a DialogFragment (not a Fragment):

    val yourDialogFragment = YourDialogFragment()
    yourDialogFragment.show(
        activity.supportFragmentManager,
        "YOUR_TAG_FRAGMENT"
    )
    

    Under the hood, the show() method does create a FragmentTransaction and adds the tag by using the add() method. But it's much more convenient to use the show() method in my opinion.

    You could shorten it for Fragment too, by using a Kotlin Extension :)

提交回复
热议问题