为了片段或不碎片 - 嵌套碎片对活动。我为什么要使用一个以上的活动? [英] To Fragment or not to Fragment - Nested Fragments against Activities. Why should I use more than one Activity?

查看:97
本文介绍了为了片段或不碎片 - 嵌套碎片对活动。我为什么要使用一个以上的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于是否应该使用讨论活动片段的。 例如:

There are a lot of discussions about whether you should use Activities or Fragments. For example:

  • To-Fragment-or-not-to-Fragment
  • One Activity and all other Fragments
  • How many Activities vs. Fragments
  • Use Activity or Fragment in Android Application

大多数我发现讨论的Andr​​oid 4.2之前被释放。
与Android 4.2谷歌发明嵌套的片段的。

Most of the discussions I found were released before Android 4.2.
With Android 4.2 Google invented nested Fragments.

因此​​,我居然看不出有任何理由再使用多个活动

Therefore I actually don't see any reason anymore to use more than one Activity.

片段初期他们应该是在应用程序支持的用片智能手机的同时一个舒适的方式。

In the early stage of Fragments they were supposed to be used within Apps for supporting Tablets and Smartphones in a comfortable way at the same time.

因此​​,例如,你有一个的ListView 它可以在一个项目打开上点击一个细节查看。在智能手机上,我们将取代的ListView 键,显示详细的查看代替。而不是与细节视图替换名单而一个可以同时显示浏览在同一时间。

Thus for example you have a ListView which can open a detail View on click on an item. On a Smartphone we would replace the ListView and show the detailed View instead. Whereas a Tablet instead of replacing the List with the detail View can show both Views at the same time.

现在嵌套片段还有很多其他的可能性。如果你想使用一个活动,你可以存储在活动一般信息和每一个片段将有机会获得它。

Now with nested Fragments there are a lot of other possibilities. In case you want to use a single Activity, you could store general information in the Activity and every Fragment would have access to it.

除此之外,片段谁嵌套了片段,也可以存储信息,为自己的孩子片段

Besides this, Fragments who have nested Fragments, could also store information for their children Fragments.

使用片段我可以很容易地重用浏览,我可以显示多个片段在同一时间,我可以伊斯利形成一个对话框了片段的。这一切都将带我可能不会比只是一些复制和多;粘贴操作。

With Fragments I can easily reuse the Views, I can show more than one Fragment at the same time and I can easly form a dialog out of a Fragment. This all would take me probably not more than just some copy & paste actions.

如果我用活动相反,我认真地要发生很大的变化,以完成这件事。

If I use Activities instead I seriously have to change a lot to get this done.

我最近实施的应用程序,我很容易就可以使用两个片段-ViewPager 来得到的东西真的很漂亮和动感(有些种类的:今天的信息 - 昨天的资料)。 在我看来片段使我们的生活的方式更容易:)

I recently implemented an Application where I easily could use two Fragment-ViewPager to get things really beautiful and dynamic (Some kind of: Today's Information - Yesterday's Information). In my opinion Fragments make our life a way easier :)

问题:

  • 为什么要使用一个以上的活动

你能不能提供任何很好的例子,其中多个的用法活动使得使用片段的更有意义呢?

Could you provide any good example in which the usage of multiple Activities makes more sense instead of using Fragments?

  • 有没有,你没有任何选择,只能使用活动
  • 任何很好的例子

我觉得最像地图 的YouTube 和合作已经支持片段更大的框架。所以,我们不必依靠活动。 也就是它很容易对付的导航栏 TabHosts ViewPager 动作条如果你使用片段

I think most of the bigger frameworks like Maps, YouTube and co already support Fragments. So we don't have to rely on Activities. Also is it quite easy to deal with NavigationBar, TabHosts, ViewPager, ActionBar in case you use Fragments.

推荐答案

首先,我会同意你的看法,这是可以编写只使用一个活动和嵌套片段一个巨大的应用。这是软件的乐趣 - 则可以使用各种方法实现相同的功能。对我来说,使用多个活动的选择归结为我个人的preferences进行封装,可重用性和可测试性。

First off, I will agree with you that it is possible to write a huge application using only one activity and nested fragments. That's the fun of software - you can achieve the same functionality using a variety of approaches. For me, the choice to use multiple activities comes down to my personal preferences for encapsulation, reusability, and testability.

如果我有一个可以在其他应用中重用的构件,我让一个片段。例如,我的应用程序拥有一个按钮与服务器同步,我已经创建了一个使用进度条直观地显示在同步过程中定制的片断。这很容易想象另一个应用程序能够使用这个小工具,所以这就是为什么我开发它作为一个片段。

If I have a widget that can be reused in other applications, I make it a Fragment. For example, my app features a "sync with server" button and I have created a custom Fragment that uses Progress Bars to visually show the synchronization process. It's easy to imagine another application being able to use this widget, so that's why I developed it as a Fragment.

如果我切换中我的应用程序的任务,使得新的任务在概念上是独立于previous任务,然后我用一个新的活动。例如,我的应用程序的第一页会让你选择一个用户。一旦你点击了一个用户,我送你到该用户的主菜单。这对于用户主菜单显示在一个新的活动。

If I am switching tasks within my app, such that the new task is conceptually independent of the previous task, then I use a new activity. For example, the first page of my app asks you to select a user. Once you've clicked on a user, I send you to the main menu for that user. This main menu for the user is displayed in a new activity.

现在让我们来想象一个大型的,复杂的应用程序,并有一组分配给开发应用的开发者。如果该应用可以被分成单独的活动,它在概念上很容易瓜分的任务。每一个活动是自己的沙箱,所以并行开发简单和单元测试。如果有任何共同的需要,球队还是应该制定碎片和重用他们,当然。我要补充一点,code再用,不经常发生,足以在软件开发中,但如果使用得当,应该有大量可重复使用的片段。

Now let's imagine a large, complex app, and a team of developers assigned to develop that app. If the app can be divided into separate activities, it is conceptually very easy to divide up the tasks. Each activity is its own sandbox, so parallel development is simple and unit-testable. If there are any common needs, the team should still develop Fragments and reuse them, of course. I should add that code reuse does not happen often enough in software development, but if done right, there should be a lot of reusable Fragments.

现在想是时候来测试应用程序。您的测试团队可以将每个活动作为自己的黑盒子。这是更容易测试比单一的巨大的应用程序,它依赖于一个单一的活动,并吨嵌套的片段。这是,如果存在错误尤为重要。如果存在错误并不明显,至少我知道了错误的范围仅限于一个单一的活动了很多。

Now suppose it is time to test the app. Your testing team can treat each activity as its own black box. This is easier to test than a single huge app that relies on a single activity and tons of nested fragments. This is especially important if a bug exists. If a bug exists that is not obvious, at least I know the scope of the bug is limited to a single activity out of many.

总之,我的猜测是,你正在开发你的应用程序作为一个个体,因此开发的决定不会影响别人。你的观点可能会改变,如果你正在开发一个更大的团队一个应用程序,我希望我的回答让很多感光。

In summary, my guess is that you are developing your app as an individual, and therefore your development decisions do not affect anybody else. Your perspectives would likely change if you were developing an app with a larger team, and I hope my response makes a lot of sense in that light.

这篇关于为了片段或不碎片 - 嵌套碎片对活动。我为什么要使用一个以上的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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