Is it possible to pass arguments to a fragment after it's been added to an activity?

前端 未结 3 1462
借酒劲吻你
借酒劲吻你 2020-12-28 13:58

I know that when you\'re first instantiating a fragment you can pass arguments using setArguments(Bundle) and retrieve them in the fragment using getArgum

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-28 14:24

    You can just expose a method on your fragment that set whatever you want to pass to it. To call it you can e.g. retrieve the fragment from the backstack by tag or keep an instance reference around from wherever you are calling it from.

    This works nicely for me although you need to be defensive in terms of null checks and such as well as aware of the lifecyle your fragment goes through when you attach it or restart it.

    From what I can tell there is nothing in the API...

    Update: This is still true and works just fine. I found that once this is more complex it is much cleaner and easier to use something like the Otto eventbus. Highly recommended imho.

提交回复
热议问题