Android的服务与GT;活动>片段与ViewPager [英] Android Service > Activity > Fragment with ViewPager

查看:180
本文介绍了Android的服务与GT;活动>片段与ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我要声明,我一直在寻找,现在为这个问题的解决方案了三天,这可能意味着要么我没有提出正确的问题或不使用一个不错的办法。如果有,请指导我在正确的方向。

First, I'd to state that I've been searching for a solution for this problem for three days now, that may means either I'm not asking the right question or not using a good approach. If any, please guide me in the right direction.

这是情景:我有一个活动和绑定的服务。在服务保存一些数据,并根据需要处理,同时发布一些信息的持续性(持续)的通知。在活动有三个片段 ViewPager 显示的由服务处理的数据

This is the scenario: I've an Activity and a bound Service. The Service holds some data and processes it as necessary while posting a persistent (ongoing) notification with some information. The Activity has three Fragments inside a ViewPager that displays the data processed by the Service.

片段是一个列表片段,显示可用,详细信息片段活动的数据条目显示细节每一个数据和参数片段,用户可以修改数据是如何处理的。

The Fragments are a List Fragment, that shows the active data entries available, a Details Fragment that displays the details for each data and a Parameters Fragment where the user can modify how the data is processed.

[服务]< - >([活动] - > [ViewPager([列表],[详细内容],[参数])])

[Service] <-> ([Activity] -> [ViewPager([List], [Details], [Parameters])])

一切工作就好了。我的活动绑定到服务,创建 ViewPager 之后再片段获取信息的槽接口

Everything works just fine. My Activity binds to the Service, the ViewPager is created after and then the Fragments fetch information trough an Interface.

下面了有趣的部分...屏幕旋转!

Here comes the fun part... Screen Rotation!

由于服务异步绑定,当用户旋转屏幕上的片段不再有数据,因为活动被包围的服务,同时他们已在present,而不是重新创建感谢 ViewPager

As the Service binds asynchronously, when the user rotates the screen the Fragments no longer have the data because the Activity is bounding the service while they're already present and not recreated thanks to the ViewPager.

我一直试图弄清楚这一点,但似乎我没有解决这个问题的认识。我一直试图使该片段静态引用服务之前将它们设置为反弹,但我不能得到一个稳定的解决方案。

I've been trying to figure this out but it seems that I don't have the knowledge to solve it. I've tried making static references to the fragments, setting them up before the service is rebound but I can't get a stable solution.

我会使用的android:在我的清单configChanges 但也有各自的方向不同的布局

I'd be using android:configChanges in my manifest but there are different layouts for each orientation.

同样地,如果我用一个坏办法,请指导我!

Again, if I'm using a bad approach, please, guide me!

推荐答案

难建议时,我不知道你的code,但想大声....

Difficult to suggest when I don't know your code but thinking out loud....

您可以有一个从来没有显示(即无头),并有setRetainInstance(真)设置,因此它不会失去你已经设置的任何状态的工人片段。

Can you have a "worker fragment" that is never displayed (i.e headless) and has setRetainInstance(true) set so it does not lose any state you have set.

您工作者片段会绑定到该服务,而不是活动和维持一个参考吧。

Your worker fragment would bind to the service instead of the activity and maintain a reference to it.

如果你需要与你的活动进行沟通,你可以用回调做到这一点。

If you need to communicate with your Activity, you can do this with callbacks.

您其他碎片可能与工人,而不是活动沟通。

Your other fragments could communicate with the worker instead of the Activity.

这个过程将主要使活动比成您的组件的其余主办的外壳而已。因为所有的数据在保留片段举行旋转会失去什么。

This process would basically make the activity little more than a shell into which the rest of your components are hosted. Rotation would lose nothing because all data is held in the retained fragment.

这篇关于Android的服务与GT;活动&GT;片段与ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆