在Android API 11+中,谷歌发布了一个名为Fragment的新类。

在视频中,谷歌建议,只要可能(link1, link2),我们应该使用片段而不是活动,但他们没有解释确切的原因。

片段的目的是什么以及它们的一些可能的用途(除了一些可以通过简单的视图/布局轻松实现的UI示例)?

我的问题是关于片段的:

使用片段的目的是什么? 与使用活动/视图/布局相比,使用片段的优点和缺点是什么?

奖金问题:

您能否为片段提供一些真正有趣的用途?谷歌在他们的视频里没有提到的事情? 在片段和包含它们的活动之间进行通信的最佳方式是什么? 当你使用片段时,最重要的事情是什么?从你的经验中有什么建议和警告吗?


不确定你指的是什么视频,但我怀疑他们说你应该使用片段而不是活动,因为它们不能直接互换。实际上,在开发指南中有一个相当详细的条目,考虑阅读它的详细信息。

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 (e.g., 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.


#1 & #2使用片段的目的是什么 与使用片段相比,使用片段的优缺点 活动/视图/布局?

Fragments是Android用于创建可重用用户界面的解决方案。您可以使用活动和布局(例如使用include)来实现一些相同的功能。然而;fragments被连接到Android API中,从HoneyComb开始。让我详细说明;

The 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. You could probably ignore this, and do something else and clever, but you'd be working against the API, not with it. The FragmentManager handles «back» for you in a very clever way. 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. The FragmentPagerAdapter code is much cleaner than a regular adapter, and it controls instantiations of the individual fragments. Your life will be a lot easier if you use Fragments when you try to create applications for both phones and tablets. Since the fragments are so tied in with the Honeycomb+ APIs, you will want to use them on phones as well to reuse code. 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. You get the latest features like the ActionBar, with tabs, overflow, split action bar, viewpager etc.

奖金2

片段之间最好的沟通方式是意图。当你在Fragment中按下一些东西时,你通常会调用StartActivity(),并在上面有数据。意图被传递到您启动的活动的所有片段。


片段是应用程序的用户界面或行为的一部分,可以放在活动中,从而实现更模块化的活动设计。如果我们说片段是一种子活动,那也不会错。

以下是关于片段的要点:

A fragment has its own layout and its own behavior with its own lifecycle callbacks. You can add or remove fragments in an activity while the activity is running. You can combine multiple fragments in a single activity to build a multi-pane UI. A fragment can be used in multiple activities. The fragment life cycle is closely related to the lifecycle of its host activity. When the activity is paused, all the fragments available in the acivity will also be stopped. A fragment can implement a behavior that has no user interface component. Fragments were added to the Android API in Android 3 (Honeycomb) with API version 11.

欲了解更多详情,请访问官方网站碎片。


片段在某些情况下特别有用,比如我们想在所有页面中保留一个导航抽屉。你可以用你想要的任何片段来膨胀框架布局,并且仍然可以访问导航抽屉。

如果您使用了一个活动,那么您将不得不在所有活动中保留抽屉,这将导致冗余代码。这是片段的一个有趣用法。

我是Android的新手,仍然认为碎片是有帮助的。


我知道这个问题已经讨论得很激烈了,但我想再补充几点:

Frags can be used to populate Menus and can handle MenuItem clicks on their own. 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). A parent Frag with child Frags can give you further options to modulize your components. E.g. you can easily swap Frags around, put new Frags inside a Pager or remove them, rearrange them. All without your Activity knowing anything about it just focusing on the higher level stuff.


这是我在片段中发现的重要信息:

Historically each screen in an Android app was implemented as a separate Activity. This creates a challenge in passing information between screens because the Android Intent mechanism does not allow passing a reference type (i.e. object) directly between Activities. Instead the object must be serialized or a globally accessible reference made available. By making each screen a separate Fragment, this data passing headache is completely avoided. Fragments always exist within the context of a given Activity and can always access that Activity. By storing the information of interest within the Activity, the Fragment for each screen can simply access the object reference through the Activity.

来源:https://www.pluralsight.com/blog/software-development/android-fragments


片段存在于活动内部,而活动则独立存在。


Fragments存在于活动中,并具有:

它自己的生命周期 自己的布局 它自己的子片段等等。

将Fragments视为它所属的主活动的子活动,它不能单独存在,它可以一次又一次地被调用/重用。希望这对你有所帮助。


1.使用片段的目的是什么?

答: 处理器件形状因子差异。 在应用程序屏幕之间传递信息。 用户界面组织。 高级UI隐喻。


活动是应用程序中带有工具栏的全屏组件,其他的最好是片段。 一个带有工具栏的全屏父活动可以有多个窗格、可滚动页面、对话框等(所有片段),所有这些都可以从父活动访问并通过父活动进行通信。

例子:

活动A、活动B、活动C

所有的活动都需要有相同的代码重复,以显示基本 工具栏,或者从父活动继承 管理起来很麻烦)。 要从一个活动移动到另一个活动,要么所有活动都需要在内存中(开销),要么需要销毁一个活动以便打开另一个活动。 活动之间的通信可以通过intent完成。

vs

活动A,片段1,片段2,片段3:

没有代码重复,所有屏幕都有来自该活动的工具栏等。 几种方法从一个片段移动到下一个视图页,多窗格等。 活动拥有最多的数据,因此需要最少的片段间通信。如果仍然需要,可以通过接口轻松完成。 片段不需要全屏显示,在设计上有很大的灵活性。 如果不需要视图,则片段不需要扩展布局。 多个活动可以使用相同的片段。


片段可以被认为是ui元素复合树中的非根组件,而活动位于复合树(ui树)的顶部。

A rule of thumb on when not to use Fragment is when as a child the fragment has a conflicting attribute, e.g., it may be immersive or may be using a different style all together or has some other architectural / logical difference and doesn't fit in the existing tree homogeneously. A rule of thumb on when to prefer Activity over Fragment is when the task (or set of coherent task) is fully independent and reusable and does some heavy weight lifting and should not be burdened further to conform to another parent-child composite (SRP violation, second responsibility would be to conform to the composite). For e.g., a MediaCaptureActivity that captures audio, video, photos etc and allows for edits, noise removal, annotations on photos etc and so on. This activity/module may have child fragments that do more granular work and conform to a common display theme.


如果你以前写过前端,那么使用前端组件(React, Vue或Angular)。将片段视为活动中的可重用组件。


为什么碎片呢?

片段被创建来替换大多数活动用例。看看这个Android开发峰会。

什么时候使用片段而不是活动?

总是这样,除非你真的需要一个只在活动中可用的API。