Essential Android Otto Event Bus edit forum

Passing an event

This example describes passing an event using the Otto Event Bus.

To use the Otto Event Bus in Android Studio you have to insert the following statement in your modules gradle file:

dependencies {
    compile 'com.squareup:otto:1.3.8'
}

The event we’d like to pass is a simple Java object:

public class DatabaseContentChangedEvent {
    public String message;

    public DatabaseContentChangedEvent(String message) {
        this.message = message;
    }
}

We need a Bus to send events. This is typically a singleton:

import com.squareup.otto.Bus;

public final class BusProvider {
    private static final Bus mBus = new Bus();

    public static Bus getInstance() {
        return mBus;
    }

    private BusProvider() {
    }
}

To send an event we only need our BusProvider and it’s post method. Here we send an event if the action of an AsyncTask is completed:

public abstract class ContentChangingTask extends AsyncTask<Object, Void, Void> {

    ...

    @Override
    protected void onPostExecute(Void param) {
        BusProvider.getInstance().post(
            new DatabaseContentChangedEvent("Content changed")
        );
    }
}

Table Of Contents
39 ACRA
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
160 Otto Event Bus
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close