This question is related to an existing question I asked. I though I’ll ask a new question instead of replying back to the other question.
“I’ve more than one Handlers in an Activity.” Why? If you do not want a complicated
handleMessage()method, then usepost()(onHandlerorView) to break the logic up into individualRunnables. MultipleHandlersmakes me nervous. — CommonsWare
I’m new to Android. My question is – is having multiple handlers in a single activity a bad design?
Here is the sketch of my current implementation.
I’ve a mapActivity that creates a data thread (a UDP socket that listens for data). My first handler is responsible for sending data from the data thread to the activity.
On the map I’ve a bunch of “dynamic” markers that are refreshed frequently. Some of these markers are video markers i.e., if the user clicks a video marker, I add a ViewView that extends a android.opengl.GLSurfaceView to my map activity and display video on this new vide. I use my second handler to send information about the marker that the user tapped on ItemizedOverlay onTap(int index) method.
The user can close the video view by tapping on the video view. I use my third handler for this.
I would appreciate if people can tell me what’s wrong with this approach and suggest better ways to implement this.
Thanks.
As I wrote in my previous comment, I would not use multiple Handler objects for that.
With respect to the UDP socket thread, you can either stick with your existing
Handler, or usepost()on yourMapViewto post aRunnableto the main application thread, or userunOnUiThread()on yourMapActivity.With respect to your “second handler to send information about the marker that the user tapped on ItemizedOverlay onTap(int index) method”,
onTap()will be called on the main application thread, and so you do not need to use aHandler. The same is true for your thirdHandler.