Skip to content

rvlopez/EventBusDemo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EventBusDemo

This is a simple demo using EventBus. EventBus is a publish/subscriber event bus optimized for Android and developed by Markus Junginger in Greenrobot.

EventBus in 3 steps

  1. Define events:

    public class GlobalBus {
        private static EventBus eventBus;
    
        public static EventBus getEventBus() {
            if (eventBus == null) {
                eventBus = EventBus.getDefault();
            }
            return eventBus;
        }
    }
  2. Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:

    @Subscribe(sticky = true)
    public void onMessageEvent(EventMessageList event) { /* Do something */ }

    Register and unregister your subscriber. For example on Android, activities and fragments should usually register according to their life cycle:

    @Override
    protected void onStart() {
        super.onStart();
        GlobalBus.getEventBus().register(this);
    }
    
    @Override
    protected void onStop() {
        GlobalBus.getEventBus().unregister(this);
        super.onStop();
    }
  3. Post events:

    GlobalBus.getEventBus().postSticky(new EventMessageList(messageList));

Read the full getting started guide.

About

A simple demo using EventBus library

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages