The official DEMO isonResume()注册和onPause()anti-registration. However, this mechanism may not be suitable for most demand scenarios.
If you are considering de-registering inonCreate(...)注册和onDestroy(), there is a situation you need to pay attention to:
If an Activity creates multiple instances, and the Event from EventBus.post(Event) should only be received by the top-level Activity instance, then this will cause problems.
Register in OnCreat and check whether you have registered before. Repeated registration will result in an error.
Deregister in Destory. I have unregistered it in onPause, and when the page is not visible, the refresh broadcast or other broadcasts sent out cannot be received. After receiving the instance in the top-level Activity, you can also respond by writing OnEvent directly in the View, so before registering, check whether it has been registered. Just use this inside, not Context.
The official DEMO is
onResume()
注册和onPause()
anti-registration. However, this mechanism may not be suitable for most demand scenarios.If you are considering de-registering in
onCreate(...)
注册和onDestroy()
, there is a situation you need to pay attention to:Register in OnCreat and check whether you have registered before. Repeated registration will result in an error.
Deregister in Destory. I have unregistered it in onPause, and when the page is not visible, the refresh broadcast or other broadcasts sent out cannot be received. After receiving the instance in the top-level Activity, you can also respond by writing OnEvent directly in the View, so before registering, check whether it has been registered. Just use this inside, not Context.
Who registers and who logs out? Register and unregister are the most scientific in the same category