Android- Unable to use org.greenrobot.eventbus inside of separate module

1.2k Views Asked by At

Here I'm coding for a customized calendar component module library. My end goal is to pass callback information of the selected date to the main project from the module library where the calendar is available.

I am trying to access org.greenrobot.eventbus in separate module in my project. But, getting below error. When I'm declaring @Subscribe method in MainActivity activity this is resolved. But, I want to subscribe from the module, not from the main project. Need your help to fix this issue.

org.greenrobot.eventbus.EventBusException: Subscriber class com.sayantan.weekcalender.MainActivity and its super classes have no public methods with the @Subscribe annotation

EDITED

apply plugin: 'com.android.library'

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        minSdkVersion 22
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'joda-time:joda-time:2.10.10'
    implementation 'org.greenrobot:eventbus:3.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}

WeekCalender.java placed in module file.

public class WeekCalender extends LinearLayout {
    /**
     * Reference variables
     **/
    private Context mContext;
    private OnDateClickListener mListener;
    private List<DateTime> mDateTimeList;

    public WeekCalender(Context context) {
        super(context);
        this.mContext = context;
        initDays();
        initViews(null);
    }

    public WeekCalender(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initDays();
        initViews(attrs);
    }

    public WeekCalender(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initDays();
        initViews(attrs);
    }

    private void initViews(AttributeSet attrs) {
        if (attrs != null) {
            //.....
        }

        NUM_OF_PAGES = calculateNoOfPage();
        WeekPager weekPager = new WeekPager(getContext(), attrs);
        addView(weekPager);
    }

    private void initDays() {
        //....
        //....
    }

    private int calculateNoOfPage() {
        int noOfPage = 0;
        //....
        //....
        return noOfPage;
    }

    /**
    * This method is not getting triggered here, but, this is working find in main project activity class
    */
    @Subscribe
    public void onDateClicked(Event.CalenderEvent event) {
        Toast.makeText(mContext, event.getSelectedDate().toLocalDate().toString(), Toast.LENGTH_SHORT).show();
        if (mListener != null)
            mListener.onDateClick(event.getCurrentDate());
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        if (!EventBus.getDefault().isRegistered(mContext)) {
            EventBus.getDefault().register(mContext);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        if (EventBus.getDefault().isRegistered(mContext)) {
            EventBus.getDefault().unregister(mContext);
        }
    }

    public void setSelectedDate(DateTime selectedDate) {
        EventBus.getDefault().post(new Event.SetSelectedDateEvent(selectedDate));
    }

    public void setOnDateClickListener(OnDateClickListener listener) {
        this.mListener = listener;
    }
}

Please note: Event posted from viewpager fragment inside of this module.

DaysAdapter-

// Event handler
            root.setOnClickListener(v -> {
                // change text color
                DateListUtils.setSelectedDate(item);
                EventBus.getDefault().post(new Event.CalenderEvent(dateTime, item));
                setColor(item);
            });
1

There are 1 best solutions below

0
Emir On

please replace the below code. currently, you are registered on mContext object and it does not contain any subscribers, then you need to deliver event WeekCalendar's instance instead.

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();

    if (EventBus.getDefault().isRegistered(this)) {
        EventBus.getDefault().unregister(this);
    }
}


@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    if (!EventBus.getDefault().isRegistered(this)) {
        EventBus.getDefault().register(this);
    }
}