Android:Activity.runOnUiThread 和 View.post 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10558208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: What's the difference between Activity.runOnUiThread and View.post?
提问by Alexander Kulyakhtin
What's the difference between Activity.runOnUiThread
and View.post
, could someone, please, explain?
Activity.runOnUiThread
和 和 有什么区别View.post
,有人可以解释一下吗?
回答by MByD
There is no real difference, except that the View.post
is helpful when you don't have a direct access to the activity.
没有真正的区别,除了View.post
当您无法直接访问活动时有用。
In both cases, if not on UI thread, Handler#post(Runnable)
will be called behind the scenes.
在这两种情况下,如果不是在 UI 线程上,Handler#post(Runnable)
都会在幕后调用。
As CommonsWare mentioned in the comment, there is a difference between the two - when called on Ui thread, Activity#runOnUiThread
will call the run
method directly, while View#post
will post the runnable
on the queue (e.g. call the Handler#post
)
正如CommonsWare在评论中提到的,两者是有区别的——在Ui线程上Activity#runOnUiThread
调用时,会run
直接调用方法,而View#post
会runnable
在队列中发布(例如调用Handler#post
)
The important point IMO is that both have the same goal, and for whoever use it, there should be no difference(and the implementation may change in the future).
IMO 重要的一点是,两者都有相同的目标,对于使用它的人来说,应该没有区别(并且将来可能会更改实现)。
回答by pareshgoel
Another difference between Activity.runOnUiThread and view.post() is that the runnable in view.post() is called after the view is attached to a window.
Activity.runOnUiThread 和 view.post() 的另一个区别是 view.post() 中的 runnable 在视图附加到窗口后被调用。
回答by kabuko
Either are acceptable for most situations and for the most part they are interchangeable, but they aresubtly different. The biggest difference of course is that one is available from an Activity
and the other from a View
. There's a lot of overlap between those, but sometimes in an Activity
you will not have access to a View
, and sometimes in a View
you will not have access to an Activity
.
在大多数情况下它们都是可以接受的,并且在大多数情况下它们是可以互换的,但它们有细微的不同。最大的区别当然是一个可以从 an 获得Activity
,另一个可以从 a 获得View
。它们之间有很多重叠,但有时在 a 中Activity
您将无法访问 a View
,有时在 a 中View
您将无法访问Activity
。
One of the edge cases I've encountered with View.post
I mentioned in an answer to another SO question on View.post
: View.post
only works from another threadwhen the View
is attached to a window. This is rarely a problem, but can occasionally cause the Runnable
to never execute, especially if you call View.post
in the onCreate
method of your Activity
. An alternative is to use Handler.post
which is what Activity.runOnUiThread
and View.post
use under the covers anyway.
View.post
我在回答另一个 SO 问题时View.post
提到的我遇到的边缘情况之一:View.post
仅当附加到窗口时才能从另一个线程工作View
。这是很少会成为问题,但可以偶尔导致Runnable
从不执行,特别是如果你调用View.post
在onCreate
你的方法Activity
。另一种方法是使用Handler.post
which is whatActivity.runOnUiThread
并View.post
在幕后使用。
(edited for accuracy, added "from another thread")
(为准确性进行了编辑,添加了“来自另一个线程”)
回答by Pacerier
Another difference: post
is per View; runOnUiThread
is per Activity.
另一个区别:post
按视图;runOnUiThread
是每个活动。
This means it will be possible (in the future?) to do view.getQueue
/ activity.getQueue
and get exactly what you want without your own tracking or filtering code.
这意味着有可能(在未来?)在没有自己的跟踪或过滤代码的情况下做view.getQueue
/activity.getQueue
得到你想要的。