Nobody likes an unresponsive UI.
By way of explanation, what I want to do is that:
- show dialog
- wait for response from server
- close dialog on response
Provided that the UI is running in a separate thread (which it does), all I have to do is spawn one thread which does the above. Well, not so fast. Turns out that:
- In order to show a dialog, I must manually spawn new a Runnable into UI thread. Never mind that this could be done in a thread-safe way via message passing. Or the framework could be nice and do it itself.
- For some reason, dialog objects passed to UI thread have to be declared as final. But I'm going to be spawning multiple of these dialogs over the lifetime of the object, and trying to re-assign a final variable will throw an exception. So this dictates that the AlertDialog object must be created inside the UI thread's Runnable.
- And, I cannot block the UI thread waiting for the server response, so waiting for server must now go into a separate thread, spawned from within the UI Runnable. While at it, I must also take care to block the main thread, so it does not attempt to read from the socket while this thread does.
- By some miracle I am now allowed to call dialog.dismiss() from this other thread. If I wasn't, then I'd have to spawn another UI Runnable...