Designing multiple Android handlers
This question is related to the existing question I asked. I though I will ask a new question and not answer another question.
"I have more than one handler in action." What for? If you don't need a complex method
handleMessage()
, usepost()
(onHandler
orView
) to split the logic into individual onesRunnables
. A lotHandlers
makes me nervous. - CommonsWare
I am new to Android. My question is, do multiple handlers in one activity have bad design?
Here is a sketch of my current implementation.
I have mapActivity
one that creates a data stream (UDP socket that listens for data). My first handler is responsible for sending data from the data stream to the activity.
On the map, I have a bunch of "dynamic" markers that refreshed
often. Some of these markers are video markers, that is, if the user clicks on a video marker, I add ViewView
that expands android.opengl.GLSurfaceView
to my activity on the map and displays the video on this new video. I am using my second handler to send information about the token that the user clicked on the method ItemizedOverlay
onTap(int index)
.
User can close the video by clicking on the video preview. I use my third handler for this.
I would appreciate it if people could tell me what is wrong with this approach and suggest better ways to implement it.
Thanks.
a source to share
As I wrote in my previous comment, I would not use multiple Handler objects for this.
As for the UDP socket stream, you can either stick with the existing one Handler
, or use post()
on yours MapView
to send Runnable
to the main application thread, or use runOnUiThread()
on yours MapActivity
.
As for your "second handler" to send the marker information the user was listening on with the ItemizedOverlay onTap (int index) method, onTap()
will be called on the main thread of the application, and therefore you don't need to use a Handler
. The same is true for your third Handler
.
a source to share