启动器重新启动时小部件未更新
我有一个小部件,每当配置发生更改(例如屏幕方向)以及手机解锁时,它就会自行更新。此过程涉及为我的小部件上的按钮设置 onClick
处理程序。这很有效,但是我发现有一个用例导致我的应用程序不响应 onClick
事件。这种特殊情况是每当启动器自行重新启动时。
有没有办法检测启动器何时重新启动,以便我可以手动更新我的小部件?或者是否有另一种方法可以确保 onClick 处理程序不会丢失?
I've got a widget which will update itself whenever there's a configuration change (such as screen orientation), and whenever the phone is unlocked. This process involves setting onClick
handlers for the buttons on my widget. This works well, however I have found that there's a usage case which causes my app to not respond to onClick
events. This particular case is whenever the launcher restarts itself.
Is there a way to detect when a launcher restarts, so I can update my widget manually? Or is there another way to ensure onClick
handlers are not lost?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,我在发送
new RemoteViews()
垃圾邮件,而我本应该只调用它一次来生成视图,然后在需要时引用该实例。在我的解决方案中,我有一个类变量,用于存储这个单个 RemoteView 实例,以及一个用于访问它的 getter。Turns out I was spamming
new RemoteViews()
when I should have just called it once to produce the view, and then referred to that one instance when required. In my solution, I have a class variable which stores this single RemoteView instance, and a getter to access it.@Glitch 的提议可能不适用于某些情况,尤其是带有
ListView
的应用程序小部件。这是因为多次调用appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id)
后,ListView
会变得非常慢(尝试滚动ListView
) 。我的猜测是,单个
RemoteView
实例会将其所有执行的指令保留在列表中。随着时间的推移,指令列表将会不断增长。每次appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id)
时,大指令列表都会重新执行。我建议的解决方案如下。但是,我相信它仅适用于某些设备,因为并非所有设备在启动器重新启动期间都会收到相同的广播消息。
Proposal by @Glitch might not work for certain cases, especially app widget with
ListView
. This is becauseListView
will get very slow (Try to scroll theListView
) afterappWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id)
had been called several time.My guess is, the single
RemoteView
instance will keep all its executed instruction in a list. Over the time, the instruction list will grow. Every timeappWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id)
, the large instruction list will be executed all over again.My proposed solution is as follow. However, I believe it will only work on certain device, as not all devices will receive same broadcast message during launcher restarting.