Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:07:34+00:00 2026-06-18T11:07:34+00:00

Why the event is not subscribed, and onMyEvent not called. Relavent code is below.

  • 0

Why the event is not subscribed, and onMyEvent not called. Relavent code is below.
Otto is working normally in my uses, but below scenario involves posting the event from a callback handler (this is a simplification of code involving http calls). Not sure this has to do anything with it.

I use Otto (Guava event bus based) and Dagger (Guice). Hope Java experts may also be able to see any issue on how I inject and use the bus below.

Application (Module registration)

package com.example.ottocb;

import android.app.Application;
import android.content.Context;
import com.squareup.otto.Bus;
import dagger.Module;
import dagger.ObjectGraph;
import dagger.Provides;

import javax.inject.Singleton;

public class MyApplication extends Application {
    private ObjectGraph objectGraph;

    @Override
    public void onCreate() {
        super.onCreate();
        objectGraph = ObjectGraph.create(new MyModule(this));
    }

    public ObjectGraph objectGraph() {
        return objectGraph;
    }

    public void inject(Object object) {
        objectGraph.inject(object);
    }

    @Module(entryPoints = {Bus.class, MyActivity.class,
            MyFragment.class
    })
    static class MyModule {
        private final Context appContext;

        MyModule(Context appContext) {
            this.appContext = appContext;
        }

        @Provides
        @Singleton
        Bus provideBus() {
            return new Bus();
        }
    }
}

BaseFragment

package com.example.ottocb;

import android.app.Fragment;
import android.os.Bundle;

public class BaseFragment  extends Fragment {
    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);

        ((MyApplication) getActivity()
                .getApplication())
                .inject(this);
    }

}

MyFragment

package com.example.ottocb;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import javax.inject.Inject;

public class MyFragment extends BaseFragment  {
    private static final String TAG = MyFragment.class.getName();

    @Inject
    Bus bus;

    Button btn;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myfragment, container,
                false);
        btn = (Button) view.findViewById(R.id.btn);
        btn.setOnClickListener(btnOnClick);

        return view;
    }

    Button.OnClickListener btnOnClick = new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i(TAG, "onClick");
            MyCB cb = new MyCB();
            cb.success();
        }
    };

    private class MyCB  {
        public void success() {
            Log.i(TAG, "SUCCESS " );

                bus.post(new MyEvent());

        }

        public void failure() {
            Log.e(TAG, "Error");

        }
    }

    @Subscribe
    public void onMyEvent(MyEvent event) {
        Log.i(TAG, "***** onMyEvent ********");
        Toast.makeText(getActivity(), "***** RECEIVED EVENT *****", Toast.LENGTH_SHORT).show();
    }
}

MyActivity

package com.example.ottocb;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

MyEvent

package com.example.ottocb;

public class MyEvent {
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent" android:layout_height="match_parent">
    <fragment class="com.example.ottocb.MyFragment"
              android:id="@+id/myfragment"
              android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>

myfragment.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Post" />


</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:07:36+00:00Added an answer on June 18, 2026 at 11:07 am

    In order to receive events, a class instance needs to register with the bus.

     bus.register(this); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Site here . Why is it that the code works for $('#club').change(function(event) but not
I'm trying to change pageindex on page_Load event but its not working. I can
I have Window with UserControl. I subscribed for OnKeyUp event but it does not
In onchange event is not working in jquery.selectbox-05, i am using two dropdown box
The following key event is not working. Any idea? class Frame(wx.Frame): def __init__(self): wx.Frame.__init__(self,
I know there should not be multiple publishers publishing the same event. But how
KeyDown Event is not responding till any user control is clicked or setFocus implicitly
The key event is not listened by my Flex app. Since it is really
While trying to load a Bitmap onto a SWFLoader the Event.COMPLETE event is not
I am not able to apply onclick event in global custom text field. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.