Android – event listener

I hope this will be simple question.
I have main activity, on this activity I create an instance of some class. How to send some event form one class to main one? How to setup some kind a listener to send notifications between classes. Only option what I know/use right now is to keep reference to parent class and call directly some function from child class.

I’m wonder if it possible to create something like is in ActionScript, where I can call to dispatchEvent(new Event(“name”)) and later setup addEventlistener(“name” function) ??

  • Can I keep the speech recognizer listening indefinitely?
  • Draw text over sphere in libgdx
  • Retaining list in list fragment on orientation change
  • Gradle build error cannot resolve io.fabric
  • ksoap2 org.xmlpull.v1.xmlpullparserexception expected start_tag error
  • How to prevent multiple instances of an activity when it is launched with different intents
  • Related posts:

    Anything like Twitter Bootstrap for Android?
    android: AbsListView.OnScrollListener SCROLL_STATE_IDLE is not called after SCROLL_STATE_TOUCH_SCROL...
    Grant uri permission to uri in EXTRA_STREAM in intent
    Custom cut/copy action bar for EditText that shows text selection handles
    How to use robolectric to test started intent with extra data
    Which is the best layout in android we can use to design the UI?
  • Beginning Programmer Interested in Android - Should I Start with Java?
  • spinner adding string array on item selection how can get item related value in android
  • Cast shadow on top of LinearLayout using android:elevation
  • Does View.removeAllViews() release memory?
  • after pausing app
  • Notification Icon with the new Firebase Cloud Messaging system
  • 2 Solutions collect form web for “Android – event listener”

    If “I implement some class” means that you have declared a nested class inside your Activity class than nested non-static class will have a reference to parent class object.

    In general, you can always create dispatcher/listener pattern your self. Create listener interface and add either addListener or setListener method to class that will dispatch event.

    Example of listener:

    public interface IAsyncFetchListener extends EventListener {
        void onComplete(String item);
        void onError(Throwable error);

    Example of event dispatcher:

    public class FileDownloader {
        IAsyncFetchListener fetchListener = null;
        private void doInBackground(URL url) {
            if (this.fetchListener != null)
        public void setListener(IAsyncFetchListener listener) {
            this.fetchListener = listener

    Example of class with event listener:

    public class MyClass {
        public void doSomething() {
            FileDownloader downloader = new FileDownloader();
            downloader.setListener(new IAsyncFetchListener() {
                public void onComplete(String item) {
                    // do something with item
                public void onError(Throwable error) {
                    // report error

    Just implement a listener(or a list of listeners) on the class that generates the events.

    When an event is generated iterate over this list and call a method that all the listeners must implement (via an interface maybe?)

    Hope it helped,

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.