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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:10:07  来源:igfitidea点击:

Android: What's the difference between Activity.runOnUiThread and View.post?

android

提问by Alexander Kulyakhtin

What's the difference between Activity.runOnUiThreadand View.post, could someone, please, explain?

Activity.runOnUiThread和 和 有什么区别View.post,有人可以解释一下吗?

回答by MByD

There is no real difference, except that the View.postis 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#runOnUiThreadwill call the runmethod directly, while View#postwill post the runnableon the queue (e.g. call the Handler#post)

正如CommonsWare在评论中提到的,两者是有区别的——在Ui线程上Activity#runOnUiThread调用时,会run直接调用方法,而View#postrunnable在队列中发布(例如调用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 Activityand the other from a View. There's a lot of overlap between those, but sometimes in an Activityyou will not have access to a View, and sometimes in a Viewyou 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.postI mentioned in an answer to another SO question on View.post: View.postonly works from another threadwhen the Viewis attached to a window. This is rarely a problem, but can occasionally cause the Runnableto never execute, especially if you call View.postin the onCreatemethod of your Activity. An alternative is to use Handler.postwhich is what Activity.runOnUiThreadand View.postuse under the covers anyway.

View.post我在回答另一个 SO 问题时View.post提到的我遇到的边缘情况之一:View.post仅当附加到窗口时才能从另一个线程工作View。这是很少会成为问题,但可以偶尔导致Runnable从不执行,特别是如果你调用View.postonCreate你的方法Activity。另一种方法是使用Handler.postwhich is whatActivity.runOnUiThreadView.post在幕后使用。

(edited for accuracy, added "from another thread")

(为准确性进行了编辑,添加了“来自另一个线程”)

回答by Pacerier

Another difference: postis per View; runOnUiThreadis per Activity.

另一个区别:post按视图;runOnUiThread是每个活动。

This means it will be possible (in the future?) to do view.getQueue/ activity.getQueueand get exactly what you want without your own tracking or filtering code.

这意味着有可能(在未来?)在没有自己的跟踪或过滤代码的情况下做view.getQueue/activity.getQueue得到你想要的。