困境:当使用片段VS活动:困境、片段、VS

2023-09-12 00:58:12 作者:把心崶住

是的,我知道这听起​​来像起初一个愚蠢的问题,我也知道,活动旨在重新present我的应用程序的一个屏幕,而片段被设计为与逻辑可重用的UI布局嵌入其中的内部。

Yes, I know it sounds like a dumb question at first and I also know that Activities are designed to represent a single screen of my application, while Fragments are designed to be reusable UI layouts with logic embedded inside of them.

直到不久前,我开发了一个应用程序,因为它说,他们应该得到发展。 我创建了一个活动来重新present我的应用程序的屏幕,并使用片段为 ViewPager 谷歌地图。我很少创建了一个 ListFragment 或其它可多次重复使用的用户界面。

Until not long ago, I developed an application as it said that they should be developed. I created an Activity to represent a screen of my application and used Fragments for ViewPager or Google Maps. I rarely created a ListFragment or other UI that can be reused several times.

最近我无意中发现,其中包含一个项目只有2 活动一个是 SettingsActivity 键,另外一个是 MainActivity 。的布局 MainActivity 填充了很多隐藏的全屏幕的UI片段,并只显示一个。在 Acitivty 逻辑有许多 FragmentTransitions 应用程序的不同屏幕之间。

Recently I stumbled on a project that contains only 2 Activities one is a SettingsActivity and other one is the MainActivity. The layout of the MainActivity is populated with many hidden full screen UI fragments and only one is shown. In the Acitivty logic there are many FragmentTransitions between the different screens of the application.

我喜欢这个方法的缺点是,因为应用程序使用动作条,它保持完整,不影响画面切换的动画,这与活动切换。这给了更流畅的感觉,这些屏幕切换。

What I liked about this approach is that because the application uses an ActionBar, it stays intact and does not move with the screen switching animation, which is what happens with Activity switching. This give a more fluent feel to those screen transitions.

所以我猜我问的是分享关于这个话题您目前的发展方式,我知道这可能看起来像第一眼看上去的意见基础的问题,但我把它作为一个Android设计和架构问题...不是一个真正的基于观点的。

So I guess what I'm asking is to share your current development manner regarding this topic, I know it might look like an opinion based question at first look but I look at it as an Android design and architecture question... Not really an opinion based one.

更新(2014年1月5日):在此之后presentation通过埃里克·伯克从广场,(这是我要说的是一个伟大的presentation着大量的Andr​​oid开发人员有用的工具,而我以任何方式广场我没有关系)

UPDATE (01.05.2014): Following this presentation by Eric Burke from Square, (which I have to say is a great presentation with a lot of useful tools for android developers. And I am not related in any way to Square)

http://www.infoq.com/$p$psentations/Android-Design/

从我个人的经验,在过去的几个月中,我发现,构建我的应用程序的最佳方法是创建来重新present一个流应用程序中的片段组, present在一个活动所有这些片段。所以基本上你会在你的应用程序流的数量活动相同数量。 这样的操作栏保持不变对流量的全屏幕,但被重新上改变流动,这使得有很大的意义。正如埃里克·伯克州和我已经认识到,以及,用尽可能少的的哲学活动尽可能并不适用于所有情况,因为它在什么创建了一个烂摊子,他所谓的神的活动。

From my personal experience over the past few months, I found that the best way to construct my applications is to create groups of fragments that come to represent a flow in the application and present all those fragments in one Activity. So basically you will have the same number of Activities in your application as the number of flows. That way the action bar stays intact on all the flow's screens, but is being recreated on changing a flow which makes a lot of sense. As Eric Burke states and as I have come to realize as well, the philosophy of using as few Activities as possible is not applicable for all situations because it creates a mess in what he calls the "God" activity.

推荐答案

专家会告诉你:当我看到的用户界面,我就知道是否使用活动片段。在开始的时候这不会有任何意义,但假以时日,你会真正能够告诉你需要片段与否。

Experts will tell you: "When I see the UI, I will know whether to use an Activity or a Fragment". In the beginning this will not have any sense, but in time, you will actually be able to tell if you need Fragment or not.

这是一个很好的做法我觉得很帮我。它发生在我,而我是想解释一下我的女儿。

There is a good practice I found very helping for me. It occurred to me while I was trying to explain something to my daughter.

也就是说,想象一个盒子内的再presents的屏幕。您可在这个盒子装载另一个屏幕?如果你使用一个新的框,你将有多个项目从第1框中复制?如果答案是肯定的,那么你应该使用片段,因为根的活动可以容纳所有重复的元素,以节省您创建它们的时候,你可以简单地更换零部件框。

Namely, imagine a box which represents a screen. Can you load another screen in this box? If you use a new box, will you have to copy multiple items from the 1st box? If the answer is Yes, then you should use Fragments, because the root Activity can hold all duplicated elements to save you time in creating them, and you can simply replace parts of the box.

但不要忘了,你总是需要一箱箱(活动)或部分将被分散。所以一箱里面的部件。

But don't forget that you always need a box container (Activity) or your parts will be dispersed. So one box with parts inside.

小心不要滥用框。 Android的UX专家提醒(你可以找到他们在YouTube上)时,我们应该明确加载另一个活动,而不是用一个片段(当我们处理导航抽屉里面有类别,如)。一旦你觉得舒服的片段,您可以观看所有影片。更是强制性的材料。

Take care not to misuse the box. Android UX experts advise (you can find them on YouTube) when we should explicitly load another Activity, instead to use a Fragment (like when we deal with the Navigation Drawer which has categories). Once you feel comfortable with Fragments, you can watch all their videos. Even more they are mandatory material.

可你现在看看你的用户界面,并找出如果你需要一个活动片段?你得到一个新的角度?我想你没有。 :)

Can you right now look at your UI and figure out if you need an Activity or a Fragment? Did you get a new perspective? I think you did. :)