为什么使用片段? [英] Why use Fragments?

查看:138
本文介绍了为什么使用片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的优势是什么,使用片段结束了使用自定义的查看 s表示在不同的布局重用?

What is the advantage to using Fragments over using custom Views that are reused in different layouts?

原来的博客文章介绍片段中,戴安娜Hackborn说这

In the original blog post introducing fragments, Dianne Hackborn says that

[片段]让开发者更容易编写可扩展应用程序   在各种尺寸的屏幕,已经超越设施   可在该平台。

[Fragments] make it easier for developers to write applications that can scale across a variety of screen sizes, beyond the facilities already available in the platform.

和她接着做出了一个应用程序,结合了两项活动的UI来自同一个应用程序的手机版本的平板电脑布局的背景下解释片段。

and she goes on to explain Fragments in the context of making a tablet layout for an app that combines the UI of two activities from the phone version of the same app.

但似乎相同的重用,可以使用自定义视图来实现的。片段和视图之间的主要不同似乎是,他们具有不同的生命周期...

But it seems that the same reuse could be achieved using custom Views. The main different between Fragments and Views seems to be that they have differing lifecycles...

片段的生命周期是:

onAttach()的onCreate() onCreateView() onActivityCreated() ONSTART() onResume()的onPause()的onStop() onDestroyView()的onDestroy() onDetatch()

onAttach(), onCreate(), onCreateView(), onActivityCreated(), onStart(), onResume(), onPause(), onStop(), onDestroyView(), onDestroy(), onDetatch().

查看的生命周期是:

男星 onFinishInflate() onAttachedToWindow() onMeasure() onLayout() onDetatchedFromWindow()

我想从开发人员听到有经验编写大型应用程序什么好处(如果有的话),他们已经看到了使用片段VS自定义视图瓜分UI成可重复使用的部分。

I'd like to hear from developers with experience writing large apps about what benefits (if any) they've seen in using Fragments vs custom Views to divide up the UI into reusable pieces.

推荐答案

主要的原因是,片段比自定义视图的可重用性。

有时候,你不能创建单靠看法完全封装的UI组件。这是因为有一些事情,你会想投入你的看法,但不能因为只有一个活动可以处理它们,从而迫使一个活动和一个视图之间的紧耦合。

Sometimes you can't create a fully encapsulated UI component relying on views alone. This is because there are things you would want to put into your view but can't because only an Activity can handle them, thus forcing tight coupling between an Activity and a View.

下面就是这样一个例子。比方说,你要创建的是,在许多事情,想拍摄照片​​,并用它做什么一个可重复使用的UI组件。传统上,你会解雇启动相机和拍摄的图像返回意图。

Here is one such example. Lets say you want to create a reusable UI component that, among many things, want to capture a photo and do something with it. Traditionally you would fire an intent that starts the camera and returns with the captured image.

请注意,您的自定义UI组件不能完全封装这一功能,因为它必须依赖于主办活动的 startActivityForResult ,因为意见不接受活动结果(他们可以间接地烧透方面的意图)。

Notice that your custom UI component can't fully encapsulate this functionality because it will have to rely on hosting Activity's startActivityForResult because views don't accept activity results (they can indirectly fire an intent through context).

现在,如果你想重用你的自定义UI组件在不同的活动,你会被重复$ C $下Activity.startActivityForResult。

Now if you wanted to reuse your custom UI component in different activities you would be repeating the code for Activity.startActivityForResult.

片段另一方面干净地解决这个问题。

Fragment on the other hand cleanly solve this problem.

同样的片段可以促进项目的选项菜单,传统的东西只有一个活动可以做的。再次,如果您的自定义视图的状态决定了发生的事情在菜单中,这可能是非常重要的。

Similarly your fragment can contribute items to your options menu, something traditionally only an Activity could do. Again this could be important if the state of your custom view dictates what goes in the menu.

这篇关于为什么使用片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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