I'm new to Android development. There seem to be 2 important classes when dealing with threading in Android: Looper
and Handler
. There are static Looper.myLooper()
and Looper.getMainLooper()
methods to get the current thread's looper and the UI thread's looper, respectively. However, there are no such static methods for Handler
. If you want to post to the UI thread, for example, code samples suggest doing this:
new Handler(Looper.getMainLooper()).post(new Runnable() { ...
Why doesn't Handler
expose a cached, static getMainHandler()
method that looks like this? Wouldn't that avoid creating unnecessary garbage?
private static final Handler mainHandler = new Handler(Looper.getMainLooper());
public static Handler getMainHandler() { return mainHandler; }