Skip to content
This repository has been archived by the owner on Dec 25, 2019. It is now read-only.

Getting Started

beworker edited this page Nov 16, 2014 · 3 revisions

Getting started

public class MainActivity extends Activity {

    // 1. First we need to get instance of bus attached to current
    // context, which is out activity in this case. You don't need
    // to create anything. TinyBus will create instance for you if
    // there is no bus instance for the context yet.
    
    private Bus mBus;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mBus = TinyBus.from(this);
        ...
    } 
    
    
    // 2. Every event receiver has to be registered to be able to 
    // receive events and unregister when it is not interested in
    // events anymore. The best place to do it is inside onStart()
    // and onStop() methods of activity or fragment.
    
    @Override
    protected void onStart(Bundle savedInstance) {
      super.onStart(savedInstance);
      mBus.register(this);
    }

    @Override
    protected void onStop() {
      mBus.unregister(this);
      super.onStop();
    }
  
    // 3. Now activity is able to receive events. Let's implement 
    // a callback method for an event of type LoadingEvent. 
    // Callback must be a public method with @Subscribe annotation.
    
    @Subscribe
    public void onLoadingEvent(LoadingEvent event) {
        if (event.state == LoadingEvent.STATE_STARTED) {
            // put your logic here, e.g. update ActionBar state
        }
    }
}
public class LoadingEvent {

    // 4. Here is our event class itself. An event can be any class.

    public static final int STATE_STARTED = 0;
    public static final int STATE_FINISHED = 1;
    
    public int state;
    
    public LoadingEvent(state) {
        this.state = state;
    }
}
public class LoadingFragment extends Fragment {

    // 5. Now let's have a fragment which initiates loading and 
    // posts loading event to the bus.

    private LoadingEvent mLoadingEvent;

    @Override
    public void onClick(View view) {
        // ... initiate loading first 
        
        mLoadingEvent = new LoadingEvent(
                LoadingEvent.STATE_STARTED);
        TinyBus.from(getActivity()).post(mLoadingEvent);
    }
    
    // 6. If we want to have a "sticky" event which gets posted 
    // to all listeners even if they are registered after we 
    // posted the event, we have to implement a producer method
    // as following.
    
    @Produce
    public LoadingEvent getLastLoadingEvent() {
        return mLoadingEvent;
    }
    
    // 7. To be able to produce events, producer has to be 
    // registered onto the bus. To register a fragment as a 
    // listener, we use same techniques, by calling same 
    // TinyBus.from() factory method to access the bus
    // instance.
    
    @Override
    public void onStart() {
        super.onStart();
        TinyBus.from(getActivity()).register(this);
    }
    
    @Override
    protected void onStop() {
      TinyBus.from(getActivity()).unregister(this);
      super.onStop();
    }

    // 8. Now, once user initiates loading inside our 
    // fragment, activity gets notified too.
}
public class AnotherFragment extends Fragment {

    // 9. If we have another fragment, which needs to
    // receive loading events, we can register it in 
    // the very same way.

    @Override
    public void onStart() {
        super.onStart();
        TinyBus.from(getActivity()).register(this);
    }
    
    @Override
    protected void onStop() {
      TinyBus.from(getActivity()).unregister(this);
      super.onStop();
    }

    @Subscribe
    public void onLoadingEvent(LoadingEvent event) {
        // do something with received event here
    }
}

Alternatively, if you need a single bus instance for the whole application, you have to request it from your application context. In the example below a fragment will post an event to the single event bus instance. Any receiver registered at the same bus instance will receive this event.

public class BackgroundFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        // 1. Get singleton bus instance
        TinyBus bus = TinyBus.from(getActivity()
                .getApplicationContext());
        
        // 2. Post event
        bus.post(new LoadingEvent(LoadingEvent.STATE_STARTED));
    }
  
}
Clone this wiki locally