I use the greenrobot Eventbus for events in my Android app.
I registered a subscriber and unregister them accordingly and nevertheless it calls them multiple times. It seems as if they are registered multiple times but they aren't. Also, in the log as in the method trace of the application the register and unregistered methods seem to be called in the right order and number.
It happens after I switched activity, to an empty activity, and switch back to my main activity (see code below).
[ObjectName].register();
simply contains a EventBus.getDefault().register(this);
and [ObjectName].unregister();
the according unregister method.
Main Code (truncated):
public class MainOverviewActivity extends AppCompatActivity {
private List<Task> mainTaskList;
MainTaskAdapter taskAdapter;
UserController userController;
TaskController taskController;
ApiController apiController;
Authenticator authenticator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_overview);
EventBus.getDefault().register(this);
userController = new UserController();
taskController = new TaskController(context);
apiController = new ApiController(context);
authenticator = new Authenticator(context);
// UNIMPORTANT CODE ADDED HERE
if(!EventBus.getDefault().isRegistered(userController)){
userController.register();
taskController.register();
apiController.register();
authenticator.register();
}
EventBus.getDefault().post(new OnlineModeActivateEvent());
//Here the main event chains get fired which happen multiple times
taskController.getAll();
}
@Override
public void onPause(){
Log.e("MyApp", "OnPause");
EventBus.getDefault().unregister(this);
userController.unregister();
taskController.unregister();
apiController.unregister();
authenticator.unregister();
super.onPause();
}
}