Android UI 线程消息队列调度顺序 [英] Android UI Thread Message Queue dispatch order

查看:18
本文介绍了Android UI 线程消息队列调度顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 中使用保留片段在配置更改期间保持 AsyncTask 时,我认为这是最好的方法,但我对 UI 线程的消息队列调用顺序产生了一些疑问.

While working with retain Fragments in Android to hold an AsyncTask during configuration changes, which i guess it's the best approach, some doubts appear in my mind about UI Thread's Message Queue invocation order.

例如:想象一下这个场景:

Ex: Imagine this scenario:

  1. 发生配置更改,用户旋转设备.AsyncTask 正在运行.
  2. Fragment onDetach() 被调用
  3. AsyncTask doInBackground() 方法完成
  4. AsyncTask onPostExecute()被调用
  5. Fragment onAttach() 被调用
  1. Configuration Change occurs, user rotates the device. AsyncTask is running.
  2. Fragment onDetach() is called
  3. AsyncTask doInBackground() method finishes
  4. AsyncTask onPostExecute()is called
  5. Fragment onAttach() is called

UI Thread Message Queue 也可以这样:

So can UI Thread Message Queue be like this:

队列顶部 -> onDetach() |onPostExecute() |onAttach()

Queue top -> onDetach() | onPostExecute() | onAttach()

我知道它不能,据我所知,对 onPostExecute() 的调用将等到配置更改完成,但这是如何工作的?来自活动、片段生命周期的调用是否连续执行?

I know it cannot, the call to onPostExecute() will wait until the configuration change completes, as far as i know, but how does that work ? Are the calls from Activities, Fragments life-cycles executed consecutively ?

推荐答案

onPostExecute() 不能在 Fragment#onDetach()Fragment#onAttach() 在配置更改期间.这种说法背后的原因有三个:

It is not possible for onPostExecute() to be called in between Fragment#onDetach() and Fragment#onAttach() during a configuration change. The reasoning behind this claim is threefold:

  1. 配置更改在主线程的消息队列中的单个消息中处理.

  1. Configuration changes are handled inside a single message in the main thread's message queue.

一旦 doInBackground() 方法返回,AsyncTask 就会调度 onPostExecute() 方法在主线程通过向主线程的消息队列发布消息.

As soon as the doInBackground() method returns, the AsyncTask schedules the onPostExecute() method to be invoked on the main thread by posting a message to the main thread's message queue.

配置更改的消息将包含将调用 ActivityFragment 生命周期方法(例如 onDetach()onAttach()).AsyncTask 的消息将包含调用 onPostExecute() 方法的代码.由于主线程顺序处理其消息队列中的消息,不可能同时执行两条消息,因此在调用之间永远不能调用onPostExecute()>onDetach()onAttach().

The configuration change's message will contain the code that will invoke the Activity and Fragment lifecycle methods (such as onDetach() and onAttach()). The AsyncTask's message will contain the code that will invoke the onPostExecute() method. Since the main thread processes messages in its message queue sequentially, it is impossible for the two messages to be executed at the same time, and therefore onPostExecute() can never be invoked in between the calls to onDetach() and onAttach().

阅读我在本主题中对 Doug Stevenson 的回复,以获得更详细的解释(包括链接到证明声明的源代码).

Read my response to Doug Stevenson in this thread for a more detailed explanation (including links to the source code that prove the claim).

这篇关于Android UI 线程消息队列调度顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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