Home  >  Q&A  >  body text

java - EventBus在普通对象里如何调用unregister()?

场景:在Android里,有一个Adapter,Activity里用Eventbus来post数据到这个adapter里。

问题:我可以在Adapter的构造方法里调用register()方法:

public BroadcastAdapter(Context context) {
    mContext = context;
    EventBus.getDefault().register(this);
    Log.d(TAG, "constructor");
}

但我要在哪里调用EventBus.getDefault().unregister(this)呢?

PHPzPHPz2713 days ago519

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:51:23

    There is no way to directly perceive the status of ListView in the ListAdapter corresponding to ListView. There is a pair of methods in RecyclerView.Adapterpublic void onAttachedToRecyclerView(RecyclerView recyclerView)public void onDetachedFromRecyclerView(RecyclerView recyclerView)that can achieve your needs.
    But my suggestion is to register and unregister the Event Handler where the BroadcastAdapter is constructed (usually Activity or Fragment).

    Additional answer

    According to the original poster, I want to completely encapsulate the EventBus-related things in the BroadcastAdapter. The existence of the EventBus should not be perceived by the outside world, because this is completely a BroadcastAdapter thing and has nothing to do with the users of the BroadcastAdapter, that is, decoupling .
    First of all, when is the appropriate time to call register and unregister of EventBus? My answer is when you need and don’t need event processing (nonsense...), generally the places we commonly use are onCreate, onDestory (or onResume, onPause). In the original poster’s case, it should be BroadcastAdapter is used and not When using .
    Now I assume that the poster's Adapter is the inherited RecyclerView.Adapter. The public void onAttachedToRecyclerView(RecyclerView recyclerView)public void onDetachedFromRecyclerView(RecyclerView recyclerView)是在Adapter关联及取消关联RecyclerView的时候会回调的方法。一个Adapter正在是赋给了RecyclerView才算做一个有用的Adapter,所以我觉得onAttachedToRecyclerViewonDetachedFromRecyclerView正对应的BroadcastAdapter使用及未使用的时候。但是,有一个问题,我看了下Android的源码,onDetachedFromRecyclerView只是在更换其它Adapter的时候会调用,所以你应该在不使用RecyclerView的时候setAdapter(null) mentioned before is the method that will be called back when the Adapter associates and unassociates with the RecyclerView. An Adapter is considered a useful Adapter only when it is assigned to RecyclerView, so I think onAttachedToRecyclerView and onDetachedFromRecyclerView correspond to when the BroadcastAdapter is used and when it is not used. However, there is a problem. I looked at the Android source code and found that onDetachedFromRecyclerView is only called when replacing other Adapters, so you should setAdapter(null) when not using RecyclerView.


    Actually, after saying so much, it’s not the answer I want to give - -|||, I think the poster’s design is lacking. An Adapter should conscientiously do its job (adapting data to View), and a custom View should also quietly display its view. My design plan is to put event processing in ActivityFragment. When the view needs to be updated during event processing (sometimes event processing not only needs to update the view, but may also need to update data, etc.), it is obviously inappropriate to place it in the View and Adapter. ), View provides some interfaces that can update the view. In this way, Activity is responsible for event processing and plays the role of Controller, while View is simply responsible for its view. I think this is decoupling. One of the object-oriented design principles is the Single Responsibility Principle.

    reply
    0
  • Cancelreply