Inflate a view in a background thread

后端 未结 3 584
温柔的废话
温柔的废话 2020-12-01 09:20

I have a very simple question:

Is or is it not possible to inflate a view (not add it to layout) in a background thread (ex: in the doInBack

3条回答
  •  有刺的猬
    2020-12-01 09:46

    Is or is it not possible to inflate a view (not add it to layout) in a background thread (ex: in the doInBackground of an AsyncTask)?

    Possible, yes. Recommended? No. As mentioned in the documentation:

    Thus, there are simply two rules to Android's single thread model:

    1. Do not block the UI thread
    2. Do not access the Android UI toolkit from outside the UI thread

    via: Processes and Threads

    Update [02/06/19]:

    Apparently, the support library has a tool to do this: AsyncLayoutInflater (Jetpack version). It was introduced in version 24, around 2016 (2 years after my answer)

    But, as mentioned on other answers, be careful with this tool as it can very easily backfire.

提交回复
热议问题