为什么片段,何时使用片段而不是活动?

本文翻译自:Why fragments, and when to use fragments instead of activities?

In Android API 11+, Google has released a new class called Fragment . 在Android API 11+中,Google发布了一个名为Fragment的新类。

In the videos, Google suggests that whenever possible ( link1 , link2 ), we should use fragments instead of activities, but they didn't explain exactly why. 在视频中,谷歌建议尽可能( link1link2 ),我们应该使用片段而不是活动,但他们没有解释确切原因。

What's the purpose of fragments and some possible uses of them (other than some UI examples that can be easily be achieved by simple views/layouts)? 片段的目的是什么?它们的一些可能的用途(除了一些可以通过简单的视图/布局轻松实现的UI示例)?

My question is about fragments: 我的问题是片段:

  1. What are the purposes of using a fragment? 使用片段的目的是什么?
  2. What are the advantages and disadvantages of using fragments compared to using activities/views/layouts? 与使用活动/视图/布局相比,使用片段有哪些优缺点?

Bonus questions: 奖金问题:

  1. Can you give some really interesting uses for fragments? 你能为片段提供一些非常有趣的用途吗? Things that Google didn't mention in their videos? 谷歌在他们的视频中没有提到的事情?
  2. What's the best way to communicate between fragments and the activities that contain them? 片段与包含片段的活动之间进行通信的最佳方式是什么?
  3. What are the most important things to remember when you use fragments? 使用片段时要记住哪些最重要的事情? Any tips and warnings from your experience? 您的经验提示和警告?

#1楼

参考:https://stackoom.com/question/hxrl/为什么片段-何时使用片段而不是活动


#2楼

Not sure what video(s) you are referring to, but I doubt they are saying you should use fragments instead of activities, because they are not directly interchangeable. 不确定你指的是哪些视频,但我怀疑他们是说你应该使用片段而不是活动,因为它们不能直接互换。 There is actually a fairly detailed entry in the Dev Guide, consider reading it for details. 在开发指南中实际上有一个相当详细的条目 ,请考虑阅读它以获取详细信息。

In short, fragments live inside activities, and each activity can host many fragments. 简而言之,片段存在于活动内部,每个活动都可以容纳许多片段。 Like activities, they have a specific lifecycle, unlike activities, they are not top-level application components. 与活动一样,它们具有特定的生命周期,与活动不同,它们不是顶级应用程序组件。 Advantages of fragments include code reuse and modularity (eg, using the same list view in many activities), including the ability to build multi-pane interfaces (mostly useful on tablets). 片段的优点包括代码重用和模块化(例如,在许多活动中使用相同的列表视图),包括构建多窗格界面的能力(主要用于平板电脑)。 The main disadvantage is (some) added complexity. 主要缺点是(某些)增加了复杂性。 You can generally achieve the same thing with (custom) views in a non-standard and less robust way. 通常,您可以使用非标准且不太稳健的方式使用(自定义)视图实现相同的功能。


#3楼

#1 & #2 what are the purposes of using a fragment & what are the advantages and disadvantages of using fragments compared to using activities/views/layouts? #1和#2使用片段的目的是什么?与使用活动/视图/布局相比,使用片段的优点和缺点是什么?

Fragments are Android's solution to creating reusable user interfaces. 片段是Android创建可重用用户界面的解决方案。 You can achieve some of the same things using activities and layouts (for example by using includes). 您可以使用活动和布局(例如使用包含)来实现某些相同的功能。 However; 然而; fragments are wired in to the Android API, from HoneyComb, and up. 片段从HoneyComb连接到Android API。 Let me elaborate; 让我详细说明;

  • The ActionBar . ActionBar If you want tabs up there to navigate your app, you quickly see that ActionBar.TabListener interface gives you a FragmentTransaction as an input argument to the onTabSelected method. 如果您希望选项卡在那里导航您的应用程序,您会很快看到ActionBar.TabListener接口为您提供了FragmentTransaction作为onTabSelected方法的输入参数。 You could probably ignore this, and do something else and clever, but you'd be working against the API, not with it. 你可能会忽略这一点,做一些别的和聪明的事情,但是你会反对API而不是它。

  • The FragmentManager handles «back» for you in a very clever way. FragmentManager以非常聪明的方式为您处理«返回»。 Back does not mean back to the last activity, like for regular activities. 返回并不意味着回到最后一个活动,就像常规活动一样。 It means back to the previous fragment state. 这意味着回到之前的片段状态。

  • You can use the cool ViewPager with a FragmentPagerAdapter to create swipe interfaces. 您可以将炫酷的ViewPagerFragmentPagerAdapter一起使用来创建滑动界面。 The FragmentPagerAdapter code is much cleaner than a regular adapter, and it controls instantiations of the individual fragments. FragmentPagerAdapter代码比常规适配器更清晰,并且它控制各个片段的实例化。

  • Your life will be a lot easier if you use Fragments when you try to create applications for both phones and tablets. 如果在尝试为手机和平板电脑创建应用程序时使用Fragments,您的生活将会轻松许多。 Since the fragments are so tied in with the Honeycomb+ APIs, you will want to use them on phones as well to reuse code. 由于片段与Honeycomb + API紧密相关,因此您还需要在手机上使用它们以重用代码。 That's where the compatibility library comes in handy. 这就是兼容性库派上用场的地方。

  • You even could and should use fragments for apps meant for phones only. 您甚至可以而且应该将片段用于仅适用于手机的应用程序。 If you have portability in mind. 如果你有可携带性。 I use ActionBarSherlock and the compatibility libraries to create "ICS looking" apps, that look the same all the way back to version 1.6. 我使用ActionBarSherlock和兼容性库来创建“ICS外观”应用程序,这些应用程序看起来一直回到1.6版本。 You get the latest features like the ActionBar , with tabs, overflow, split action bar, viewpager etc. 您可以获得ActionBar等最新功能,包括标签,溢出,分割操作栏,viewpager等。

Bonus 2 奖金2

The best way to communicate between fragments are intents. 在片段之间进行通信的最佳方式是意图。 When you press something in a Fragment you would typically call StartActivity() with data on it. 当您在片段中按某些内容时,通常会使用其上的数据调用StartActivity() The intent is passed on to all fragments of the activity you launch. 意图将传递给您启动的活动的所有片段。


#4楼

A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity which enable a more modular activity design. 片段是应用程序的用户界面或行为的一部分,可以放在Activity中,从而实现更模块化的活动设计。 It will not be wrong if we say a fragment is a kind of subactivity. 如果我们说片段是一种子活动就不会错。

Following are important points about a fragment: 以下是关于片段的重要观点:

  1. A fragment has its own layout and its own behavior with its own lifecycle callbacks. 片段具有自己的布局和自己的行为以及自己的生命周期回调。

  2. You can add or remove fragments in an activity while the activity is running. 您可以在活动运行时在活动中添加或删除片段。

  3. You can combine multiple fragments in a single activity to build a multi-pane UI. 您可以在单个活动中组合多个片段以构建多窗格UI。

  4. A fragment can be used in multiple activities. 片段可用于多个活动。

  5. The fragment life cycle is closely related to the lifecycle of its host activity. 片段生命周期与其宿主活动的生命周期密切相关。

  6. When the activity is paused, all the fragments available in the acivity will also be stopped. 当活动暂停时,活动中可用的所有片段也将停止。

  7. A fragment can implement a behavior that has no user interface component. 片段可以实现没有用户界面组件的行为。

  8. Fragments were added to the Android API in Android 3 (Honeycomb) with API version 11. 使用API​​版本11将片段添加到Android 3 (Honeycomb)的Android API中。

For more details, please visit the official site, Fragments . 有关详细信息,请访问官方网站Fragments


#5楼

Fragments are of particular use in some cases like where we want to keep a navigation drawer in all our pages. 片段在某些情况下特别有用,例如我们想在所有页面中保留导航抽屉。 You can inflate a frame layout with whatever fragment you want and still have access to the navigation drawer. 您可以使用您想要的任何片段为框架布局充气,并且仍然可以访问导航抽屉。

If you had used an activity, you would have had to keep the drawer in all activities which makes for redundant code. 如果您使用过某个活动,则必须将抽屉保留在所有可能产生冗余代码的活动中。 This is one interesting use of a fragment. 这是片段的一个有趣用法。

I'm new to Android and still think a fragment is helpful this way. 我是Android新手,仍然认为片段有用这种方式。


#6楼

I know this was already discussed to death, but I'd like to add some more points: 我知道这已经讨论过死亡,但我想补充一点:

  • Frags can be used to populate Menu s and can handle MenuItem clicks on their own. Frags可用于填充Menu ,并可自行处理MenuItem点击。 Thus giving futher modulation options for your Activities. 从而为您的活动提供更多调制选项。 You can do ContextualActionBar stuff and so on without your Activity knowing about it and can basically decouple it from the basic stuff your Activity handles (Navigation/Settings/About). 你可以在没有你的活动知道的情况下做ContextualActionBar等等,并且基本上可以将它与你的Activity处理的基本内容(导航/设置/关于)分开。

  • A parent Frag with child Frags can give you further options to modulize your components. 带有子Frags的父Frag可以为您提供模块化组件的更多选项。 Eg you can easily swap Frags around, put new Frags inside a Pager or remove them, rearrange them. 例如,您可以轻松地交换Frags,将新的Frags放入寻呼机或移除它们,重新排列它们。 All without your Activity knowing anything about it just focusing on the higher level stuff. 所有没有你的活动知道任何事情只关注更高层次的东西。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值