Android: Brief note on UI event model
Found something that might be useful for other Android developers. (for Android m5-rc14). It is about how the UI event propagation works in Android. Events in android bubble up from the bottom view up to the root view.
For next subsequent event in the events-chain it will be forwarded to the last successful view that successfully handled the preceding event.
Example of event chains:
KeyDown –> KeyUp
MotionEvent.Down –> MotionEvent.Move –> . . . MotionEvent.Move –> MotionEvent.Up
Suppose we have this setup in our layout.
[View1] –subtype of -> android.view.View
[ChildView1] –subtype of -> android.view.View
[GrandChildView1] –subtype of -> android.view.View
Example event sequence and interaction using event chains KeyDown and KeyUp:
0.
Initially the GrandChildView1 has a focus, and user presses a physical key.
1.
[View1]
[ChildView1]
[GrandChildView1] onKeyDown()
The onKeyDown() of GrandChildView1 is invoked.
2.
[View1]
[ChildView1] onKeyDown()
[GrandChildView1] onKeyDown() = handled = false
The onKeyDown() returns false indicating that the event is not processed. The event is passed to ChildView1 , the parent of GrandChildView1.
The onKeyDown of ChildView1 is invoked.
3.
[View1]onKeyDown()
[ChildView1] onKeyDown() = handled = false
[GrandChildView1]
Again the onKeyDown() returns false, event is passed to View1.
4.
[View1] onKeyDown() = handled = true
[ChildView1]
[GrandChildView1]
View1 successfully processed the event by returning true.
5. After sometime … user releases the key
6.
[View1] onKeyUp()
[ChildView1]
[GrandChildView1]
The key-up event is passed to the view which successfully handled key-down event, which is View1.
Suppose ChildView1 handles the key-down event then the key-down event would stop propagating to View1 and the onKeyUp() of ChildView1 is invoked when user releases the key.
What happens when the top view returns false, the event will be forwarded to the initial view.
User press a button indefinitely ..
view1
subview1[onKeyDown()] = false
view1[onKeyDown()]=false
subview1
view1
subview1[onKeyDown()]
. . .
About this entry
You’re currently reading “Android: Brief note on UI event model,” an entry on simple blog
- Published:
- 3.26.08 / 11pm
- Category:
- Uncategorized
No comments
Jump to comment form | comments rss [?] | trackback uri [?]