What is the difference between runOnUiThread method and Handler? Which one is the best to use?

北城以北 提交于 2021-01-28 20:49:35

问题


I usually use the method

 runOnUiThread (new Runnable () {

     @Override
     public void run () {

     }

 });

to launch some prcess in the main thread. just recently I discovered this one

new Handler(Looper.getMainLooper()).post(new Runnable () {

    @Override
    public void run () {
        // this will run in the main thread
    }

});

My question is what is the difference between the two methods and which one is the best to use?


回答1:


Both are actually same. Both runOnUiThread and Handler#post runs the passed Runnable in the UI Thread.

FYI, you can also execute any Runnable on UI Thread with the help of any View by calling the method View#post(runnable).

Since all approaches uses Handler internally, all are same and there won't be any difference in using any of these.



来源:https://stackoverflow.com/questions/52152906/what-is-the-difference-between-runonuithread-method-and-handler-which-one-is-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!