处理方向变化最佳实践:安卓 [英] Best practice to handle orientation change: Android
问题描述
我要通过各种措施来处理方向变化线程和AsyncTask的。我看到了以下解决方案:
I was going through various practices to handle orientation change with threads and AsyncTask. I came across following solutions:
-
连接 - 分离模式:安装和拆卸的活动线程和AsyncTask的同时preserving它们的实例。 (资料来源:<一href="https://github.com/commonsguy/cw-android/blob/master/Rotation/RotationAsync/src/com/commonsware/android/rotation/async/RotationAsync.java">1, <一href="http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working/3821998#3821998">2)
Attach-detach model : Attaching and detaching activity to threads and AsyncTask while preserving their instance. (Source: 1, 2)
无头片段的方式:使用非UI /无头的片段来完成所有的线程相关的操作和配置更改保持它的实例。 (来源: 1 ,的2 )
Headless fragment way : Using a non-UI/headless fragment to do all the thread related operations and retaining its instance on configuration change. (Source: 1, 2)
还有没有其他的方法来处理这种情况?什么是推荐的做法呢?我问这个,因为我无法找到一个通用的解决方案在Android文档的任何地方。
Are there any other approaches to handle this scenario? What is the recommended practice? I'm asking this because I couldn't find a generic solution anywhere in the Android docs.
推荐答案
有上面提到的几种方法都不错的做法,但我想我会总结起来有简短解释。下面是一些最流行的库当前正在使用HTTP网络,异步工作方式/线程和缓存。
Some summaries
There are several methods mentioned above that are good practices but I thought I might sum them up with short explanations. Below are some of the most popular libraries being used currently for http networking, asynchronous work / threading, and caching.
我个人现在使用的奥托 装载机 排球 Ormlite ,以及基于网络协议栈阿帕奇和服务
秒。我希望取代,网络堆栈在某些时候与任何排球 改造,也许最终的 Robospice
I personally am currently using Otto, Loaders, Volley, Ormlite, and a network stack based on Apache and Service
s. I do hope to replace, the network stack at some point with either Volley, Retrofit, and maybe eventually Robospice.
我的个人的非常像奥托和截击
I personally very much like Otto and Volley
- <一个href="https://github.com/octo-online/robospice">https://github.com/octo-online/robospice
- <一个href="http://www.youtube.com/watch?v=ONaD1mB8r-A">http://www.youtube.com/watch?v=ONaD1mB8r-A
- 在一个插件/模块化的方法来长时间运行的任务
- 这是像瑞士军队刀库,但是你需要知道每个工具的功能。
- 把手REST调用
- 通过定向等变化 仍然存在数据
- 可以处理磁盘和内存缓存)
- 工作与各种HTTP库和持久性库(GSON,杰克逊,春天, OkHttp ,和许多下方库)
- 公测 Ormlite 的支持,我觉得
- https://github.com/octo-online/robospice
- http://www.youtube.com/watch?v=ONaD1mB8r-A
- a plugin / modular approach to long-running tasks
- this is like the "swiss-army-knife" of libraries, but you need to know what each tool does.
- Handles REST calls
- persists data through orientation and other changes
- can handle disk and memory caching )
- works with various HTTP libraries and persistence libraries (Gson, Jackson, Spring, OkHttp, and many of the below libraries)
- beta for Ormlite support, I think
- https://github.com/square/retrofit
- 注释库,使REST非常容易。工程与Robospice。
- <一个href="https://android.googlesource.com/platform/frameworks/volley">https://android.googlesource.com/platform/frameworks/volley
- <一个href="https://developers.google.com/events/io/sessions/325304728">https://developers.google.com/events/io/sessions/325304728
- 这是一个运行在谷歌Play商店的网络code
- 快速,可靠
- 在处理大多数缓存为你提供一些有意义的默认值
- 非常好用
- 专为快速图像,JSON等内置装载
- 在处理所有线程你。
- https://github.com/square/picasso 为加载图像
- 在HTTP库
- 快速
- 非常好用
- https://github.com/square/picasso
- Http library for loading images
- fast
- very easy to use
- 很好的支持
- 在通过方向改变坚持并保存碎片状态/负载
- 可以是很难得到的权利
- 没有缓存
- 从UI线程后台工作的简单方法
- 必须被取消,小心被拆除,一个活动或片段后返回任务。
- https://github.com/square/otto
- 在事件总线,使部件和碎片容易 之间的同步工作
- 非常强大
@Produce
的能力保留了最后一个事件,并且能够产生它要求任何新的兴趣的用户到总线
- https://github.com/square/otto
- Event bus that makes a-sync work between components and fragments easy
- Very powerful
@Produce
ability retains the last event and can produce it on demand for any new interested subscribers to the bus
- 在我个人从来没有见过这种使用不是Vogella的教程等,所以我不知道这一点。
- 在老学校的方式
- 在最终控制权,你必须尽一切自己
- 在通常Appache或投诉客户端使用,
- 通经意图周围包裹
这篇关于处理方向变化最佳实践:安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!