Skip to content

cnmen/eventbus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hey, Do you really know about EventBus ?

If you not sure, please click here : EventBus

EventBus Build Status

  • EventBus is a publish/subscribe event bus for Android and Java.
  • simplifies the communication between components
  • makes your code simpler
  • is fast
  • is tiny (~50k jar)
  • has advanced features like delivery threads, subscriber priorities, etc.

EventBus in 4 steps

1. Configuration app/build.gradle:

android {
    // ......
    defaultConfig {
        // ......

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [packageName: 'com.cmonbaby.eventbus.apt', className: 'EventBusIndex']
            }
        }
    }
    buildTypes {
        release {
            // ......
        }
    }
}

2. Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:

@Subscribe(threadMode = ThreadMode.MAIN)
public void getUser(UserInfo info) { }

3. Post events:

EventBus.getDefault().post(new UserInfo("simon", 35));
// or
EventBus.getDefault().postSticky(new UserInfo("simon", 35));

4. About Coding

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // create indexes for apt
        EventBus.getDefault().addIndex(new EventBusIndex());
        // register listener
        EventBus.getDefault().register(this);
    }

    // post event
    public void post() {
        EventBus.getDefault().post(new UserInfo("simon", 35));
    }

    // post sticky event
    public void sticky() {
        EventBus.getDefault().postSticky(new UserInfo("simon", 35));
    }

    // The higher number the quantity, the higher the priority
    @Subscribe(threadMode = ThreadMode.MAIN, priority = 1)
    public void getUser(UserInfo info) { // subscribe method
        // Do something
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void sticky(UserInfo user) { // subscribe sticky method
        Log.e("sticky", user.toString());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // destroy eventbus
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        EventBus.clearCaches();
    }
}

Add EventBus to your project

Via Gradle:

implementation 'com.cmonbaby.eventbus.core:eventbus_core:1.2.0'
implementation 'com.cmonbaby.eventbus.annotation:eventbus_annotation:1.2.0'
annotationProcessor 'com.cmonbaby.eventbus.compiler:eventbus_compiler:1.2.0'

Via Maven:

<dependency>
    <groupId>com.cmonbaby.eventbus.annotation</groupId>
    <artifactId>eventbus_annotation</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>com.cmonbaby.eventbus.compiler</groupId>
    <artifactId>eventbus_compiler</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>com.cmonbaby.eventbus.core</groupId>
    <artifactId>eventbus_core</artifactId>
    <version>1.2.0</version>
</dependency>

License

Copyright (C) 2013-2020 Markus Junginger, Simon (https://www.cmonbaby.com)
EventBus binaries and source code can be used according to the Apache License, Version 2.0.