实例非UI线程视图 [英] Instantiating a View on non-UI thread

查看:251
本文介绍了实例非UI线程视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道的UI元素(查看层次)只能从UI线程进行操作。为后台操作,所述的AsyncTask可以使用,它提供了事件处理程序到达UI线程

要简短,它是允许在非UI线程创建一个视图(绑 getApplicationContext())?该自定义视图的后代 - 一旦实例化 - 从 UI线程添加到视图层次。因此,只有在构造函数调用中完成的 Asynctask.doInBackground();它的连接( addView(...))到活动的根布局层次仍然在UI线程中完成的。

在细节:

 公共MyView的扩展视图{
     公共MyView的(上下文的背景下){
            ...
     }
...
}


  1. 我做了一个自定义的视图,与被覆盖的的onDraw(...)等。


  2. 当用户在我的主要活动点击某个菜单项,另一个活动(MyOtherActivity)被创建和显示的这屏幕是完全MyView的


  3. 由于MyOtherActivity的屏幕必须立即显示出来,我美元一个的AsyncTask而用户是其他地方的主要活动p $ P-MyView的实例化(即他没有点击的菜单项还)。该MyView的引用存储在静态数据成员


  4. MyOtherActivity.onCreate()被调用时,它的构造code MyView的需要从静态,并通过<把它添加到其布局层次根源code> addView(...)。


  5. (我知道,静态变量可能会引入内存泄漏,所以我将它设置为一旦它的没有必要的。)


这难道不是一个问题(可能是导致异常的问题)的MyView的是在不同的线程实例化(并采取的返回值getApplicationContext()在其构造函数)?


解决方案

明确的答案文档中出现的对的视图,在标题事件处理和线程


  

注意:整个视图树是单线程的。你必须总是在UI线程调用的任何视图中的任何方法时。如果你正在做的其他线程的工作,并希望以更新该线程的视图状态,你应该使用一个处理程序


所以,它不只是东西,显然会影响UI的外观,如 addView(),但任何的方法的任何的视图。

在Android开发者的讨论的是@CommonsWare链接到了从Android框架小组确认,这是要认真对待一个以上的高水平工程师。

I know that the UI elements (View hierarchy) may only be manipulated from the UI thread. For a background operation, the AsyncTask can be used, which offers event handlers to reach the UI thread.

To be brief, is it allowed to instantiate a View (tied to getApplicationContext()) in a non-UI thread? This custom View descendant -- once instantiated -- is added to the view hierarchy from the UI thread. So only the constructor call is done inside an Asynctask.doInBackground(); it's attaching (addView(...)) to the Activity's root layout hierarchy is still done in the UI thread.

In details:

public MyView extends View {
     public MyView(Context context) { 
            ...
     }
...
}

  1. I made a custom View, with overriden onDraw(...) etc.

  2. When the user clicks a certain MenuItem in my main Activity, another Activity (MyOtherActivity) is created and displayed which screen is exactly MyView

  3. Since the screen of MyOtherActivity must be displayed instantly, I pre-instantiate MyView in an AsyncTask while the user is somewhere else in the main Activity (i.e. he hasn't clicked that MenuItem yet). The MyView reference is stored in a static data member.

  4. When MyOtherActivity.onCreate() is called, its constructor code takes MyView from the static, and adds it to its layout root hierarchy via addView(...).

  5. (I'm aware that the static variable may introduce memory leaks, so I set it to null once it's not needed.)

Isn't it a problem (and might it introduce unexpected issues) that MyView is instantiated in a different thread (and takes the return value of getApplicationContext() in its constructor)?

解决方案

The definitive answer appears in the documentation for View, under the heading "Event Handling and Threading":

Note: The entire view tree is single threaded. You must always be on the UI thread when calling any method on any view. If you are doing work on other threads and want to update the state of a view from that thread, you should use a Handler.

So it's not just things that obviously affect the appearance of the UI, like addView(), but "any method on any View".

The discussion on android-developers that @CommonsWare linked to has more than one high-level engineer from the Android framework team confirming that this is to be taken seriously.

这篇关于实例非UI线程视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆