Communicating between a fragment and an activity - best practices

后端 未结 9 1304
予麋鹿
予麋鹿 2020-11-22 02:45

This question is mostly to solicit opinions on the best way to handle my app. I have three fragments being handled by one activity. Fragment A has one clickable element th

9条回答
  •  情歌与酒
    2020-11-22 03:40

    There are severals ways to communicate between activities, fragments, services etc. The obvious one is to communicate using interfaces. However, it is not a productive way to communicate. You have to implement the listeners etc.

    My suggestion is to use an event bus. Event bus is a publish/subscribe pattern implementation.

    You can subscribe to events in your activity and then you can post that events in your fragments etc.

    Here on my blog post you can find more detail about this pattern and also an example project to show the usage.

提交回复
热议问题