java-活动期间的后台工作销毁或重新启动

java-活动期间的后台工作销毁或重新启动,第1张

概述我在Android开发人员中阅读过Caution:Anotherproblemyoumightencounterwhenusingaworkerthreadisunexpectedrestartsinyouractivityduetoaruntimeconfigurationchange(suchaswhentheuserchangesthescreenorientation),whichmaydestroy

我在Android开发人员中阅读过

Caution: Another problem you might encounter when using a worker
thread is unexpected restarts in your activity due to a runtime
configuration change (such as when the user changes the screen
orIEntation), which may destroy your worker thread. To see how you can
persist your task during one of these restarts and how to properly
cancel the task when the activity is destroyed, see the source code
for the Shelves sample application.

从提供的code中我无法清楚地理解

当我有与Ui线程相关的后台工作,因此可以在更改配置期间停止时-

在重启和销毁期间如何正确处理工作线程?

解决方法:

如果您的背景工作与Activity相关联,则应使用AsyncTask,它提供了cancel方法.在任务中,您可以检查isCancelled以了解是否退出 *** 作.通过AndroID重新创建活动后,您将有机会再次启动该任务.

如果您的背景工作没有任何特定的活动或UI元素,则应使用IntentService并从应用程序中的任何位置运行该服务.这样,背景工作将不会因方向改变而中断.

有时使用的快速修复/黑客方法是配置AndroID,以便在方向更改时不重新创建活动-您可以使用AndroIDManifest.xml中活动的androID:configChanges元素来执行此 *** 作.

总结

以上是内存溢出为你收集整理的java-活动期间的后台工作销毁或重新启动全部内容,希望文章能够帮你解决java-活动期间的后台工作销毁或重新启动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1076985.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存