Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) ??

like image 837
goodm Avatar asked Apr 27 '12 14:04

goodm


People also ask

What is an event listener in Android?

An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI.

How do listeners work in Android?

Android Listeners are used to capture events. When, for instance, the user interacts with the Android system by clicking on a button, the Listeners would prompt the underlying activity to do the task associated with the button click.

What does an event listener do?

An event listener is a procedure in JavaScript that waits for an event to occur. The simple example of an event is a user clicking the mouse or pressing a key on the keyboard.

How do you call a listener on Android?

Yes, you may call the listener, which in the pattern above is YourActivity . Exactly, YourActivity = listener. So, you may even call this listener's onClick(View) callback method.


1 Answers

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)
            this.fetchListener.onComplete(result);
    }

    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
            }
        });

        downloader.start();
    }
}
like image 99
Emperor Orionii Avatar answered Oct 04 '22 21:10

Emperor Orionii