Skip to content

tmtron/green-annotations

Repository files navigation

Green Annotations

An Android Annotations plugin to support the Greenrobot Eventbus.
It can inject the default EventBus and automatically handle the registration/unregistration.

Build Status Maven Central Android Arsenal Javadocs license Join the chat at https://gitter.im/green-annotations/Lobby

Usage in your Android Studio project:

In the build.gradle file of the module project:

    dependencies {
        // Android Annotations for Greenrobot
        apt 'com.tmtron:green-annotations:1.0.2'
        compile 'com.tmtron:green-annotations-api:1.0.2'
    }

Note: Green-Annotations version 1.0.2 requires Android-Annotations version 4.4.0

Details

Before

Shows the Activity without using AndroidAnnotations or GreenAnnotations:

    public class ActivityBefore extends Activity {
    
        EventBus eventBus;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            eventBus = EventBus.getDefault();
        }
    
        @Override
        public void onStart() {
            super.onStart();
            eventBus.register(this);
        }
    
        @Override
        public void onStop() {
            eventBus.unregister(this);
            super.onStop();
        }
    
        public void fireEvent(String message) {
            eventBus.post(new MessageEvent(message));
        }
    
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onMessageEvent(MessageEvent event) {
            Toast.makeText(getApplicationContext(), event.message, Toast.LENGTH_SHORT).show();
        }
    
    }

After

Shows the same Activity with AndroidAnnotations and GreenAnnotations:

    @EActivity
    public class ActivityAfter extends Activity {
    
        @EventBusGreenRobot
        EventBus eventBus;
    
        public void fireEvent(String message) {
            eventBus.post(new MessageEvent(message));
        }
    
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onMessageEvent(MessageEvent event) {
            Toast.makeText(getApplicationContext(), event.message, Toast.LENGTH_SHORT).show();
        }
    
    }

Note: green-annotations-test is a tiny test-project: a sample android app with only one single activity to show how to use green-annotations (and the gradle-setup).

License

This plugin is under the Apache 2.0 license. Copyright 2016, Martin Trummer