Android TextView.setText() invoked & returned before Thread.sleep() blocks until sleep() returns. Why?

前端 未结 2 1755
走了就别回头了
走了就别回头了 2020-12-11 10:32

In the Android framework, if a TextView\'s setText() method is called, and after it returns Thead.sleep() is called, then the screen of the device does not display the given

2条回答
  •  隐瞒了意图╮
    2020-12-11 11:31

    1)Never sleep that long on the UI thread. In fact, never sleep on it at all. That causes it to block and makes the phone unresponsive

    2)setText calls invalidate on the view. That will cause the framework to draw the view again as soon as its able- but that means needs to finish the current things its doing and get back to the main Looper (basically, it needs to return from whatever method in your code it first calls). Until then, the change won't be visible on screen.

    Edit: to put it another way, inside the framework we have

    do{
        Message msg = nextMessage();
        if(msg.what == ON_CREATE){
           currentActivity.onCreate();
        }
        else if(msg.what == DRAW){
            rootView.onDraw();
            //draw entire screen
        }
        else 
          ...  //Thousands of more events like touches, handlers, etc
    

    It can't get to the draw message until its done with the current message. Its single threaded.

提交回复
热议问题