I have an Android widget that makes quite a bit of network requests in it's onUpdate
method. My widget is expected to refresh once a day. It always does very heavy Canvas
drawing using an AsyncTask
.
When I turn my device from the portrait orientation to the landscape orientation and back to the portrait orientation, it seems that the onUpdate
method is invoked again and all those heavy network requests are issued again.
Can I prevent my application from re-updating when the orientation is changed if it is possible?
My widget is expected to refresh once a day
You do not have complete control over that. The home screen is welcome to trigger onUpdate()
calls more frequently, for whatever reason.
Can I prevent my application from re-updating when the orientation is changed if it is possible?
No, but as Kuffs suggests, you can cut down on the expense of the update process:
Or, just ignore the onUpdate()
call entirely, if it is too soon and you feel confident that the home screen has not somehow lost your prior RemoteViews
. Personally, I would regenerate the RemoteViews
using cached content, just in case some home screen implementation discarded your prior RemoteViews
(eg, process was terminated and restarted).
Also, please use an IntentService
, not an AsyncTask
, for processing long onUpdate()
work. There is nothing keeping your process in memory once onUpdate()
returns otherwise, and your process may be terminated before your work completes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.