2

this is my code

HandlerThread handlerThread = new HandlerThread("test");
        handlerThread.start();
Handler handler_test = new Handler(handlerThread.getLooper());
handler_test.post(()->{
            Log.d("handlerr_test", String.valueOf(Looper.getMainLooper()==Looper.myLooper()));
            surfaceView.setVisibility(View.GONE);
            Log.d("handlerr_test", String.valueOf(Looper.getMainLooper()==Looper.myLooper()));
        });

this code logged "false", but surfaceView is gone successfully. the code in handler_test.post() should run in child thread test,but whysurfaceView.setVisibility(View.GONE)could update ui thread widget successfully?

NEW found: now i found that if i replace handler_test.post(run) with handler_test.postDelayed(run,50) or handler_test.postDelayed(run,100),this code would fail updating the ui widget surfaceView. But with low delay,such as 10ms, handler_test.postDelayed(run,10),this code would still update the ui widget successfully.

However,in any case,this code logged "false",showed that this code run in child thread.So why this code could update ui thred after low delay but not after high delay?

zadaji
  • 141
  • 6
  • Now I assume that ``surfaceView.setVisibility(View.GONE)`` could run with no error in low delay,maybe because the **surfaceView** is not attached to ui thread in this short period. So this code did not modify the UI widget in the UI thread, it just modified the state of **surfaceView** in the child thread. – zadaji Sep 07 '22 at 11:31

0 Answers0