Fragment Callbacks vs EventBus in Android

前端 未结 2 1449
遇见更好的自我
遇见更好的自我 2021-01-15 05:11

I have faced with the problem. My Activity is fragment container, so to communicate between activity and fragments I used common Callback approach.

2条回答
  •  无人共我
    2021-01-15 05:55

    For simple one Activity to one Fragment hierarchy, callback is simplest decision to go. But think about Activity containing a Fragment, and the Fragment contains swipe-able ViewPager, and each tab of ViewPager has Fragments A,B,C.

    The Fragment A,B,C will go to long journey to send event to mother Activity, and it can be lost interface connectivity between Activity and children when they are restored during crazy complex Android Activity-Fragment lifecycle dances. In this case, eventbus like otto can be a good choice.

    The drawback of event bus approach is, it is hard to maintain where the event is come from. So, keeping a few sender is recommended.

提交回复
热议问题