Nick
Nick

Reputation: 2948

Clickable Views are asychronous?

Does views in android like Buttons,Menu buttons,Menu Drawer button are asychronous?

What iam asking is if those views when clicked are executed at the next cycle.

Thank you.

Upvotes: 0

Views: 57

Answers (2)

Michael Krause
Michael Krause

Reputation: 4859

Yes, asynchronous.

If you set a breakpoint in your onClick code, you'll see somewhere down in the stack trace something like:

at android.view.View$PerformClick.run
at android.os.Handler.handleCallback
at android.os.Handler.dispatchMessage
at android.os.Looper.loop
at android.app.ActivityThread.main

This should clue you into the fact that onClick is being invoked from a Looper and Handler - in this case the main thread's Looper and Handler.

The details about how the lower-level touch events are captured from the hardware and handled by Android aren't public and are done in native code.

But what you should count on is that View click events are invoked on the main (UI) thread.

Upvotes: 1

Ben P.
Ben P.

Reputation: 54204

When you tap a View with an OnClickListener attached, a message is posted to the main thread's message queue. Often this will feel instantaneous, but it is possible to get certain UI events to happen in an order you don't expect due to this async nature.

For example, imagine a Button that disables a second Button when you tap it. It is tehcnically possible (though very difficult), to tap each button quickly enough that the tap on the second button is posted to the main thread's message queue before the first message is handled (before the second button is disabled).

Upvotes: 1

Related Questions