Search code examples
androidgreenrobot-eventbus

Greenrobot Eventbus holds variables


I have problem with Eventbus with following symptoms. I have activity which starts another activity. There is used Eventbus and boolean value set. This value is changed during activity run. For first run is all ok, but in second run, I got bad value changed in previous run. Here is code:

public class ListArchiveTabs extends FragmentActivity {

private boolean isStartFragment = true;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    isStartFragment = true;
    EventBus.getDefault().registerSticky(this);
    System.out.println("Create archive activity " + isStartFragment);
}

@Override
public void onDestroy()
{
    super.onDestroy();
    EventBus.getDefault().unregister(this);
    System.out.println("Destroy archive activity");
}

public void onEventMainThread(GetArchiveEvent event)
{
    if(isStartFragment == true) {
        isStartFragment = false;
    } else {
    }
}

}

In first run is isStartFragment true, but other runs shows false.


Solution

  • onEventMainThread the method you have it running in the main thread, the first time you run it no any event, when it happens again, to be executed in the same thread, is executing sequentially why the flag is changed to false, change the event to an execution on a thread or change the logic of the flag remember that a sticky is an ongoing event and register it that way, look if there is an event of this kind, and to be then directly calls his onEvent therefore executed sequentially and the flag is changed