Android:屏幕唤醒(和锁定)后未调用 onRestoreInstanceState

发布于 2025-01-06 07:59:22 字数 436 浏览 1 评论 0原文

我在自定义视图中遇到问题。关闭屏幕并重新打开后,其状态不会保存。

当我关闭屏幕时,onSaveInstanceState 被正确调用,但当我打开屏幕时,onRestoreInstanceState 未被调用。我知道这是因为活动没有重新创建,但我不知道还能用什么来恢复视图的状态。

我想我需要在 onStart() 或 onResume() 处做一些事情,但我不想在活动中执行此操作,而是在视图中执行此操作,因为视图在多个活动中使用,这会非常不方便。

我尝试过其他几种方法,例如 onWindowsVisibilityChanged,但没有运气。

我还可以使用什么其他方法?

谢谢。

编辑:详细信息:我想保留的只是一个布尔值,让我知道按钮是否打开或关闭,以便我可以更改它的外观。我知道它应该在没有我干预的情况下保持其外观,但实施有点错误。

I have a problem in a custom view. Its state is not saved after I turn off the screen and turn it back on.

onSaveInstanceState is called correctly when I turn the screen off, but when I turn the screen on, onRestoreInstanceState is not called. I understand that it's because the activity is not recreated, but I don't know what else to use to restore the state of the view.

I think I would need to do something at onStart() or onResume(), but I don't want to do this in the activity, but in the view, because the view is used in multiple activities and it would be very inconvenient.

I've tried several other methods, like onWindowsVisibilityChanged, but no luck.

What other method could I use?

Thank you.

EDIT: Details: what I want to keep is only a boolean to let me know if the button is toggled on or off, so I can change it's appearance. I know it should keep its appearance without my intervention, but the implementation is a little buggy.

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

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

发布评论

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

评论(1

仅此而已 2025-01-13 07:59:22

经过更多尝试,我设法在 onWindowFocusChanged(Boolean hasWindowFocus) 中获取了我需要的事件。就我而言,它为我提供了与 onResume() 相同的机会,因为它是在窗口变得可见并置于前面时调用的。

After some more trying, I managed to get the event I needed in onWindowFocusChanged(Boolean hasWindowFocus). As far as I'm concerned, it gives me the same opportunity as onResume(), because it's called just as the window becomes visible and is brought to front.

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