它是更好地加上几个任务分成几个片段结合,而不是做所有的工作在一个片段? [英] Is it better to separate several coupled tasks into several coupled fragments instead of doing all work in one fragment?

查看:109
本文介绍了它是更好地加上几个任务分成几个片段结合,而不是做所有的工作在一个片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我的英语水平。

这是好来分隔数加上任务?[如的加载数据 - (与重新加载数据的能力或异常消息)>显示数据的]成几个片段,加上

Is it good to separate several coupled tasks [ such as loading data -> displaying data (or exception message with ability to reload data) ] into several coupled fragments ?

很多时候我必须做的是这样的:

Very often I have to do something like:


  • 做点什么长,得到的结果(例如,装载大数据)

  • 如果发生异常,则显示异常信息,并给予重新加载数据的用户的能力(例如,简单的按钮的刷新的)

  • 否则,显示数据

例如,在谷歌Play商店的。起初,有一个与服务器的一些沟通。如果Android设备是从互联网连接,然后显示有关它的信息和用户给予重新加载数据的机会。否则,显示应用内容。

For example, in the Google Play Store. At first there is some communication with server. If an android device is off the internet connection then displaying message about it and user giving the chance to reload data. Otherwise, displaying app content.

我怎样通常做在这种情况下。

How I usually do in such cases.

在活动,我有一些容器(通常是一个),这和三种不同的片段(做点什么长,显示的结果,显示除外)。

In activity I have some container (usually one) for this and three different fragments (to do something long, to display result, to display exception).

要做些什么长,我创建 TaskFragment setRetainInstance(真)(为了不处理的屏幕旋转的等)

To do something long I create TaskFragment with setRetainInstance(true) (in order not to handle screen rotations etc.)

如果我得到一个结果,那么我将 TaskFragment ResultFragment (我在其中传递的结果)。

If I get a result, then I replace TaskFragment with ResultFragment (in which I pass the result).

如果我得到一个异常然后我替换 TaskFragment ExceptionFragment 。在 ExceptionFragment 我秀异常的描述和简单的按钮的再试的。在点击这个按钮我重新启动 TaskFragment (更换 ExceptionFragment TaskFragment )。

If I get an exception then I replace TaskFragment with ExceptionFragment. In ExceptionFragment I show exception description and simple button Try again. On click on this button I restart TaskFragment (replacing ExceptionFragment with TaskFragment).

                        _result________ [ ResultFragment ]
                       |   
                       |           
                       |
                       |
_request______[ TaskFragment ]
               |       |
               |       |
               |       exception
               |       request
               |       |_______________ [ ExceptionFragment ]
               |                                     |
               |                                     |
               |___________________request___________|

不幸的是我最近来到了这方面的经验。在此之前,我用一个片段来加载和显示加载的数据。所以我不能使用 setRetaintInstance(真),因为如果我用它那么我不能使用不同的布局为不同的设备配置(旋转等)。我甚至写了复杂的<一个href=\"http://stackoverflow.com/questions/23264609/fragment-capable-to-start-long-running-tasks-and-receive-results-even-after-conf?rq=1\">fragment一流的管理屏幕的旋转,这样不会打断AsyncTasks 这是我用来加载和显示加载的数据。

Unfortunately I came to this experience recently. Before that I used one fragment to load and display loaded data. So I couldn't use setRetaintInstance(true), because if I used it then I couldn't use different layouts for different device configurations (rotations, etc.). I even wrote complicated fragment class managing screen rotations so not to interrupt AsyncTasks which I used to load and to display loaded data.

现在,当我来到我的新的解决方案,我发现它好多了,很容易,比我以前使用。它还提供工作良好的分解。

Now, when I came to my new solution, I found it much better and easily than I used before. It also provides good decomposition of work.

你怎么看待这个问题?有没有更好的办法呢?

What do you think about that? Is there any better way to do it?

推荐答案

我看不出什么毛病此方法。在无形的​​片段,具有数据是precisely要在这两种情况下该怎么做。它具有保持超越左右翻转屏幕数据的优势。你不必重新加载它,如果你要分离的片段。它使您的活动内的数据,所以你不必绑定到一个服务。它还保持逻辑一类片段,并从你的UI从而增加长期的可维护性的路程。

I don't see anything wrong with this method. Having your data in a viewless Fragment is precisely what you want to do in either case. It has the advantage of keeping your data around beyond screen flips. You don't have to reload it if you were to detach your Fragment. It keeps your data inside your Activity so you don't have to bind to a Service. It also keeps the logic to a Fragment class and away from your UI which increases long-term maintainability.

您可以考虑的唯一一件事(再次,这取决于你的设计)是没有TaskFragment处理任何UI有关。这意味着,而不是TaskFragment加入 ExceptionFragment ResultFragment ,你有活动处理它。这样的 TaskFragment 的唯一工作就是处理数据。这将在UI逻辑从数据模型类分开。你可以注册回调到 TaskFragment 的活动和片段可以在各自的重视 onResume()方法,然后卸下在的onPause()

The only thing you may consider (again, it's up to your design) is to not have the TaskFragment handle anything UI related. What this means is, instead of the TaskFragment adding ExceptionFragment or ResultFragment, you have the Activity handle it. That way the TaskFragment's only job is to handle the data. It would separate the UI logic from the data model class. You can register callbacks to the TaskFragment that the Activity and Fragments can attach to in their respective onResume() methods then detach in onPause()

这篇关于它是更好地加上几个任务分成几个片段结合,而不是做所有的工作在一个片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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