What is meaning of boolean value returned from an event-handling method in Android

John Wang picture John Wang · Sep 21, 2010 · Viewed 44.3k times · Source

In android, most event listener methods return a boolean value. What is that true/false value mean ? what will it result in to the subsequence events ?

class MyTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        logView.showEvent(event);
        return true;
    }
}

Regarding to the above example, if return true in onTouch method,I found every touch event(DOWN,UP,MOVE,etc) has been captured according to my logView. On the contrary,if return false, onely the DOWN event been captured. So it's seemd that return false will prevent the event to propagate. Am I correct ?

Furthermore, in a OnGestureListener, many methods have to return a boolean value too. Do they have the same meaning ?

Answer

adamp picture adamp · Sep 21, 2010

If you return true from an ACTION_DOWN event you are interested in the rest of the events in that gesture. A "gesture" in this case means all events until the final ACTION_UP or ACTION_CANCEL. Returning false from an ACTION_DOWN means you do not want the event and other views will have the opportunity to handle it. If you have overlapping views this can be a sibling view. If not it will bubble up to the parent.