简体   繁体   中英

How can I prevent my widget from re-updating every time the device's orientation is changed?

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:

  • Cache the results of the network I/O to disk
  • Cache the bitmap (which is what I presume the "very heavy Canvas drawing" results in) to disk

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM