q126y q126y - 5 months ago 46
Android Question

How does calling Snackbar.make() from non-UI thread works?

I call

Snackbar.make
from background thread of
AsyncTask
, it works. I thought UI operations are allowed from UI thread only[1]. But that definitely is not the case here.

How does it work? If there are certain exceptions to statement 1, what are they?

Answer

Snackbar calls under the hood a SnackbarManager, which operates on the main looper:

private SnackbarManager() {
    mLock = new Object();
    mHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            switch (message.what) {
                case MSG_TIMEOUT:
                    handleTimeout((SnackbarRecord) message.obj);
                    return true;
            }
            return false;
        }
    });
}

Link: SnackbarManager.java

Comments