困境:何时使用片段与活动: [英] Dilemma: when to use Fragments vs Activities:

查看:26
本文介绍了困境:何时使用片段与活动:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Activities 旨在代表我的应用程序的单个屏幕,而 Fragments 旨在成为可重用的 UI 布局,其中嵌入了逻辑.>

直到不久前,我开发了一个应用程序,因为它说应该开发它们.我创建了一个 Activity 来表示我的应用程序的屏幕,并为 ViewPagerGoogle Maps 使用了 Fragments.我很少创建可以重复使用多次的 ListFragment 或其他 UI.

最近我偶然发现了一个只包含 2 个 Activity 的项目,一个是 SettingsActivity,另一个是 MainActivity.MainActivity 的布局填充了许多隐藏的全屏 UI 片段,并且只显示了一个.在Activity逻辑中,应用程序的不同屏幕之间有很多FragmentTransitions.

我喜欢这种方法的一点是,因为应用程序使用了一个 ActionBar,它保持完整并且不会随着屏幕切换动画而移动,这就是 Activity 切换.这给那些屏幕转换带来了更流畅的感觉.

所以我想我要问的是分享您当前关于这个主题的开发方式,我知道它乍一看可能看起来像是一个基于意见的问题,但我将其视为一个 Android 设计和架构问题......并不是真正基于意见的.

更新 (01.05.2014):SquareEric Burke 的演示之后,(我不得不说这是一个很棒的演示)为安卓开发者提供了很多有用的工具.我与 Square 没有任何关系)

http://www.infoq.com/presentations/Android-Design/

根据我过去几个月的个人经验,我发现构建应用程序的最佳方法是创建一组片段,这些片段代表应用程序中的流程,并呈现所有这些片段在一个 Activity 中.因此,基本上您的应用程序中的 Activities 数量将与流的数量相同.这样,操作栏在所有流程的屏幕上都保持不变,但会在更改流程时重新创建,这很有意义.正如埃里克·伯克 (Eric Burke) 所说,我也逐渐意识到,尽可能少地使用 Activity 的哲学并不适用于所有情况,因为它会在他所谓的上帝"中造成混乱.活动.

解决方案

专家会告诉你:当我看到UI时,我就会知道是使用Activity还是Fragment".一开始这没有任何意义,但随着时间的推移,您实际上能够判断是否需要 Fragment.

我发现有一个很好的做法对我很有帮助.当我试图向我的女儿解释一些事情时,我突然想到了这一点.

也就是说,想象一个代表屏幕的盒子.你能在这个盒子里加载另一个屏幕吗?如果您使用新盒子,您是否需要从第一个盒子中复制多个项目?如果答案是肯定的,那么您应该使用 Fragments,因为根 Activity 可以保存所有重复的元素以节省创建它们的时间,并且您可以简单地替换部分盒子.

但是不要忘记你总是需要一个盒子容器(Activity),否则你的零件会散开.所以一盒里面有零件.

注意不要滥用盒子.Android UX 专家建议(您可以在 YouTube 上找到它们)何时应该显式加载另一个 Activity,而不是使用 Fragment(就像我们处理具有类别).一旦您对 Fragments 感到满意,您就可以观看他们的所有视频.更重要的是,它们是强制性材料.

您现在可以查看您的 UI 并确定您是否需要 ActivityFragment 吗?你有没有获得新的视角?我想你做到了.

I 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.

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.

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 Activity logic there are many FragmentTransitions between the different screens of the application.

What I like 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.

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.

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/presentations/Android-Design/

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 helpful for me. It occurred to me while I was trying to explain something to my daughter.

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.

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.

这篇关于困境:何时使用片段与活动:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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