简体   繁体   中英

why do some android events stop propagation and other don't need to?

why do some event listeners return boolean and some don't?

what is the meaning of stopping propagation ?

for instance:

public void onClick(View v)

public boolean onTouch(View v, MotionEvent event)

Have a look here: http://developer.android.com/guide/topics/ui/ui-events.html
They have a note which says:

Note: Android will call event handlers first and then the appropriate default handlers from the class definition second. As such, returning true from these event listeners will stop the propagation of the event to other event listeners and will also block the callback to the default event handler in the View. So be certain that you want to terminate the event when you return true.

What I understand about 'stopping propagation' is this: say you have a TextView nested deep in other ViewGroup like RelativeLayout and LinearLayout , a click on the text view is automatically a click on the ViewGroup . This click is then 'propagated' down till it reaches the target text view. My understanding is based on this:

Remember that hardware key events are always delivered to the View currently in focus. They are dispatched starting from the top of the View hierarchy, and then down, until they reach the appropriate destination. If your View (or a child of your View) currently has focus, then you can see the event travel through the dispatchKeyEvent() method. As an alternative to capturing key events through your View, you can also receive all of the events inside your Activity with onKeyDown() and onKeyUp().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM