PerformTraversals 和 onConfigurationChanged 中的竞争条件会导致布局不正确?

发布于 2024-09-08 20:07:34 字数 1290 浏览 6 评论 0原文

我有一个中等复杂的 Android 应用程序,我自己通过 onConfigurationChanged 处理旋转,而不是重新启动 Activity。使用设备或模拟器一段时间(多次旋转)后,我开始看到应用程序间歇性地布局不正确(在横向模式下它保留了纵向宽度)

我向活动中的所有“onAction”事件添加了跟踪日志记录语句,并且我看到两个不同的流程:

正确:

  1. onConfigurationChanged
  2. onMeasure (使用旧宽度)
  3. onLayout
  4. onMeasure (使用新宽度)
  5. onSizeChanged
  6. onLayout
  7. onDraw

当布局为不正确 我看到这个流程

不正确:

  1. onMeasure (使用新宽度)
  2. onSizeChanged
  3. onLayout
  4. onDraw
  5. onConfigurationChanged
  6. onMeasure (使用新宽度)
  7. onLayout
  8. onDraw

查看消息我想我看到了部分问题:我看到 3 条重要消息被发布:

  • MessageType 1003 --> mWinFrame MessageType 1000 的新大小
  • -->新的测量/布局通道(使用mWinFrame的大小)
  • MessageType 118 -->配置更改,启动 onConfigurationChanged。

当布局正确时,顺序似乎是 118、1003、1000。当布局错误时,顺序似乎是 1003、1000、118,因此在 onConfigurationChanged 发生之前会使用新宽度进行测量。

为了额外的混乱,当布局不正确时,当我附加层次结构查看器并转储层次结构时,它会立即变得正确,但无论我请求重新布局和/或在 onPreDraw 中无效多少次,它都不会变得正确。

问题:

1)即使进行了无序调用,我最终还是得到了 onConfigurationChanged 并请求以新的宽度和高度进行重新布局,为什么这不会导致屏幕上的更新?

2)为什么这些消息的发布顺序会随着时间的推移而改变?我从来没有在新启动时看到过这个,但当手机/模拟器“运行了一段时间”时开始看到它。

I've got a moderately complex Android application where I'm handling rotation myself through onConfigurationChanged rather than restarting the activity. After using the device or emulator for a while (many rotations) I start to see the application laying out incorrectly intermittently (it retains it's portrait width while in landscape mode)

I added trace logging statements to all the "onAction" events in the activity and I see two distinct flows:

Correct:

  1. onConfigurationChanged
  2. onMeasure (with old width)
  3. onLayout
  4. onMeasure (with new width)
  5. onSizeChanged
  6. onLayout
  7. onDraw

When the layout is incorrect I see this flow

Incorrect:

  1. onMeasure (with new width)
  2. onSizeChanged
  3. onLayout
  4. onDraw
  5. onConfigurationChanged
  6. onMeasure (with new width)
  7. onLayout
  8. onDraw

Looking at the messages getting dispatched I think I see part of the problem: I see 3 important messages getting posted:

  • MessageType 1003 --> New Size for mWinFrame
  • MessageType 1000 --> New Measure/Layout pass (uses the size of mWinFrame)
  • MessageType 118 --> Configuration Change, kickoff a onConfigurationChanged.

When the layout is correct the order seems to be 118, 1003, 1000. When it's wrong the order seems to be 1003, 1000, 118, so it gets measured with the new width before onConfigurationChanged happens.

For extra bonus confusion, when the layout is incorrect it immediately becomes correct when I attach hierarchy viewer and dump the hierarchy, but does not become correct no matter how many times I request relayout and/or invalidate in onPreDraw.

Questions:

1) Even given the out-of-order calling, I do get the onConfigurationChanged eventually and request a relayout which happens with the new width and height, why would that not result in an update on screen?

2) Why would the posting order of those messages change over time? I never see this on a fresh boot, but start to see it when the phone / emulator has been "running for a while".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再见 2024-09-15 20:07:34

这种情况似乎在速度较慢的手机上更常见,避免这种情况的一种方法是在 onPause() 中执行可怕的组装... setVisible(false) 并发布 Runnable 来执行 setVisible(true) onResume() 。

这使得它有时间在测量/布局之前注册正确的高度,您的层次结构查看器线索表明有一种神奇的方法,可能不是这个,但至少它现在似乎解决了问题。

This appears to happen more often on slower phones, one way to circumvent it is to perform a horrible kludge... setVisible(false) in onPause() and post a Runnable to do a setVisible(true) onResume().

This gives it time to register the correct height before measuring/laying out, your hierarchyviewer clue suggests there is a magic method, probably not this one, but at least it seems to duct-tape the problem for now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文