我什么时候应该在Android应用程序中使用片段?为什么使用片段?(复制)

时间:2021-10-18 07:08:34

This question already has an answer here:

这个问题已经有了答案:

I know why to use fragments and the Android developer site good explains this. But in most cases I want on the tablets the different parts to have their own special behavior and UI and I don't know how fragments can help. In most cases I think it is quicker to create 2 different Activities (1 for tablets and 1 for handsets) and to share the common behaviors and events in a third class. So keeping this in mind why should I use fragments ?

我知道为什么要使用片段,Android开发者网站good解释了这一点。但在大多数情况下,我希望在平板电脑上不同的部分有自己的特殊行为和UI,我不知道片段如何能有所帮助。在大多数情况下,我认为创建2个不同的活动(1个用于平板电脑和1个手机),并在第三个类*享常见的行为和事件会更快。记住这一点,为什么要使用片段呢?

5 个解决方案

#1


70  

Fragments are more of a UI benefit in my opinion. It's convenient for the user sometimes to see two different views of two different classes on the same screen. If, in your moment of creativity, you decide it would be nice to display your application with, say, a listView that takes up half the screen and a webView that takes up the other half - so that when you click on a list item in fragment A it passes an intent to the webView in fragment B, and suddenly you see what you just clicked without the app switching activities - then you could use a fragment. That's just an example I came up with off the top of my head.

在我看来,片段更像是UI的好处。用户有时可以方便地在同一个屏幕上看到两个不同类的不同视图。在你的创造力的时刻,如果你决定来显示您的应用程序就好了,说,listView,占用一半的屏幕和占用的webView另一半——所以,当你点击列表项片段通过一个目的片段的webView B,突然你看到你刚才点击没有应用程序交换活动,那么您可以使用片段。这只是我想到的一个例子。

Bottom line: Fragments are two or more activities on the screen at the same time.

底线:片段同时是屏幕上的两个或多个活动。

#2


57  

The benefits I see when using fragments are:

我在使用片段时看到的好处是:

  • Encapsulation of logic.
  • 封装的逻辑。
  • Better handle of the lifecycle of the fragment.
  • 更好地处理片段的生命周期。
  • Reusable in other activities.
  • 可重用在其他活动。

The drawbacks I see are:

我看到的缺点是:

  • More code(For example, instantiating a fragment manager, adding the fragment transaction, writing the callbacks of the fragment)
  • 更多的代码(例如,实例化一个片段管理器,添加片段事务,编写片段的回调)
  • Communication between fragments and activities is harder. As @jonney said it, you would need to deal with a parcelable interface to serialize your objects you wish to pass.
  • 片段和活动之间的通信比较困难。正如@jonney所说,您需要处理一个可分配的接口来序列化您希望传递的对象。

So, when deciding to use a fragment, I would ask myself the following questions:

所以,当我决定使用一个片段时,我会问自己以下问题:

  • Is the lifecycle of the fragment different from the activity's lifecycle?
  • 片段的生命周期与活动的生命周期不同吗?

If the lifecycle is different, you get better handling of the lifecycle using a fragment. For example, if you want to destroy the fragment, but not the activity. Such is the case, when you have a pager adapter.

如果生命周期不同,您可以使用片段更好地处理生命周期。例如,如果您希望销毁片段,而不是活动。当您有一个寻呼机适配器时,情况就是如此。

  • Is the fragment going to be used in several activities?
  • 这个片段将被用于多个活动吗?

The user input events will be reusable if you use a fragment.

如果使用片段,用户输入事件将是可重用的。

  • Is the amount of communication between the fragment and the activity small?
  • 片段和活动之间的通信量是否很小?

If you need to pass big objects to the fragment, you would need to deal with the code that serializes them. Also, if you need to communicate between fragment and activity, you would probably need to implement interfaces. This, in most cases, adds complexity to your codebase. It's not a difference maker, but a criteria to take into account.

如果需要将大对象传递给片段,则需要处理序列化它们的代码。此外,如果您需要在片段和活动之间进行通信,您可能需要实现接口。在大多数情况下,这会增加代码库的复杂性。它不是产生差异的因素,而是考虑的标准。

#3


14  

Google advises you to ALWAYS use Fragments.

谷歌建议您总是使用片段。

Why? It's simple:

为什么?很简单:

In the simplest case, Fragments are used like containers of activities.

在最简单的情况下,片段被用作活动容器。

Why do you need this? Again, it's simple.

你为什么需要这个?再一次,这是简单的。

Android 4 (ICS) supports both Smartphones and Tablets. This means the SAME application will be running on a smartphone and a tablet and they are likely to be very different.

Android 4同时支持智能手机和平板电脑。这意味着同样的应用程序将在智能手机和平板电脑上运行,它们很可能非常不同。

Tablets have big screens which will be empty or unused - unless you assign it properly.

平板电脑的大屏幕将是空的或未使用的——除非你正确地分配它。

That means- Putting two fragments on one activity like Contact List and Contact Info.

这意味着——将两个片段放在一个活动上,比如联系人列表和联系信息。

The smatphone will display contact List, and on a touch- display the contact's Info.

smatphone将显示联系人列表,并在触摸屏上显示联系人信息。

On a tablet, the user will still see the list and the info will be next to it.

在平板电脑上,用户仍然会看到列表,信息就在它旁边。

2 fragments- on one screen....

2片段-在一个屏幕上....

Smart? yes... supposed to be back compatible down to Android 1.6......

聪明吗?是的……应该可以回到Android 1.6……


#############################################################

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

O.K, Already Knew That? then - just try to understand the case solved:

O。K,已经知道了吗?然后——试着去理解这个案子解决了:

A lot of things work that way- list & details, Menus and Sub-Menus, Info, Detailed Info and some more detailed info. You want a way to keep it natural and smooth for a tablet which you expect to preform that way, but can't expect smartphone to display it all like the tablet did...

很多东西都是这样工作的——列表和细节,菜单和子菜单,信息,详细信息和一些更详细的信息。你想要一种让平板电脑保持自然流畅的方式,你希望它能以这种方式呈现出来,但不能指望智能手机能像平板电脑那样显示出来……

Get it?

明白了吗?

for more Information, check out this. I really think you just need to catch the concept....

要了解更多信息,请查看这里。我真的觉得你只需要抓住这个概念....

#4


6  

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.

历史上,Android应用程序的每个屏幕都是作为单独的活动来实现的。这给在屏幕之间传递信息带来了挑战,因为Android意图机制不允许在活动之间直接传递引用类型(例如对象)。相反,必须对对象进行序列化或提供全局可访问引用。

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://softwareengineering.stackexchange.com/questions/244771/why-use-android-fragments

https://softwareengineering.stackexchange.com/questions/244771/why-use-android-fragments

#5


0  

Fragment primary support more dynamic & Large UI Screen like Tablet.Because Tablet screen is much larger than normal Handset. There is more room to combine & Interchange UI Component.

碎片主要支持更动态和更大的UI屏幕,如平板电脑。因为平板电脑的屏幕比普通手机大得多。有更多的空间来合并和交换UI组件。

Fragment allow such design without the need for such complex change in the View hierarchy.

片断允许这样的设计,而不需要在视图层次结构中进行如此复杂的更改。

By divide activity layout in fragment, we become able to modify activity's appearance at runtime

通过在片段中划分活动布局,我们可以在运行时修改活动的外观

#1


70  

Fragments are more of a UI benefit in my opinion. It's convenient for the user sometimes to see two different views of two different classes on the same screen. If, in your moment of creativity, you decide it would be nice to display your application with, say, a listView that takes up half the screen and a webView that takes up the other half - so that when you click on a list item in fragment A it passes an intent to the webView in fragment B, and suddenly you see what you just clicked without the app switching activities - then you could use a fragment. That's just an example I came up with off the top of my head.

在我看来,片段更像是UI的好处。用户有时可以方便地在同一个屏幕上看到两个不同类的不同视图。在你的创造力的时刻,如果你决定来显示您的应用程序就好了,说,listView,占用一半的屏幕和占用的webView另一半——所以,当你点击列表项片段通过一个目的片段的webView B,突然你看到你刚才点击没有应用程序交换活动,那么您可以使用片段。这只是我想到的一个例子。

Bottom line: Fragments are two or more activities on the screen at the same time.

底线:片段同时是屏幕上的两个或多个活动。

#2


57  

The benefits I see when using fragments are:

我在使用片段时看到的好处是:

  • Encapsulation of logic.
  • 封装的逻辑。
  • Better handle of the lifecycle of the fragment.
  • 更好地处理片段的生命周期。
  • Reusable in other activities.
  • 可重用在其他活动。

The drawbacks I see are:

我看到的缺点是:

  • More code(For example, instantiating a fragment manager, adding the fragment transaction, writing the callbacks of the fragment)
  • 更多的代码(例如,实例化一个片段管理器,添加片段事务,编写片段的回调)
  • Communication between fragments and activities is harder. As @jonney said it, you would need to deal with a parcelable interface to serialize your objects you wish to pass.
  • 片段和活动之间的通信比较困难。正如@jonney所说,您需要处理一个可分配的接口来序列化您希望传递的对象。

So, when deciding to use a fragment, I would ask myself the following questions:

所以,当我决定使用一个片段时,我会问自己以下问题:

  • Is the lifecycle of the fragment different from the activity's lifecycle?
  • 片段的生命周期与活动的生命周期不同吗?

If the lifecycle is different, you get better handling of the lifecycle using a fragment. For example, if you want to destroy the fragment, but not the activity. Such is the case, when you have a pager adapter.

如果生命周期不同,您可以使用片段更好地处理生命周期。例如,如果您希望销毁片段,而不是活动。当您有一个寻呼机适配器时,情况就是如此。

  • Is the fragment going to be used in several activities?
  • 这个片段将被用于多个活动吗?

The user input events will be reusable if you use a fragment.

如果使用片段,用户输入事件将是可重用的。

  • Is the amount of communication between the fragment and the activity small?
  • 片段和活动之间的通信量是否很小?

If you need to pass big objects to the fragment, you would need to deal with the code that serializes them. Also, if you need to communicate between fragment and activity, you would probably need to implement interfaces. This, in most cases, adds complexity to your codebase. It's not a difference maker, but a criteria to take into account.

如果需要将大对象传递给片段,则需要处理序列化它们的代码。此外,如果您需要在片段和活动之间进行通信,您可能需要实现接口。在大多数情况下,这会增加代码库的复杂性。它不是产生差异的因素,而是考虑的标准。

#3


14  

Google advises you to ALWAYS use Fragments.

谷歌建议您总是使用片段。

Why? It's simple:

为什么?很简单:

In the simplest case, Fragments are used like containers of activities.

在最简单的情况下,片段被用作活动容器。

Why do you need this? Again, it's simple.

你为什么需要这个?再一次,这是简单的。

Android 4 (ICS) supports both Smartphones and Tablets. This means the SAME application will be running on a smartphone and a tablet and they are likely to be very different.

Android 4同时支持智能手机和平板电脑。这意味着同样的应用程序将在智能手机和平板电脑上运行,它们很可能非常不同。

Tablets have big screens which will be empty or unused - unless you assign it properly.

平板电脑的大屏幕将是空的或未使用的——除非你正确地分配它。

That means- Putting two fragments on one activity like Contact List and Contact Info.

这意味着——将两个片段放在一个活动上,比如联系人列表和联系信息。

The smatphone will display contact List, and on a touch- display the contact's Info.

smatphone将显示联系人列表,并在触摸屏上显示联系人信息。

On a tablet, the user will still see the list and the info will be next to it.

在平板电脑上,用户仍然会看到列表,信息就在它旁边。

2 fragments- on one screen....

2片段-在一个屏幕上....

Smart? yes... supposed to be back compatible down to Android 1.6......

聪明吗?是的……应该可以回到Android 1.6……


#############################################################

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

O.K, Already Knew That? then - just try to understand the case solved:

O。K,已经知道了吗?然后——试着去理解这个案子解决了:

A lot of things work that way- list & details, Menus and Sub-Menus, Info, Detailed Info and some more detailed info. You want a way to keep it natural and smooth for a tablet which you expect to preform that way, but can't expect smartphone to display it all like the tablet did...

很多东西都是这样工作的——列表和细节,菜单和子菜单,信息,详细信息和一些更详细的信息。你想要一种让平板电脑保持自然流畅的方式,你希望它能以这种方式呈现出来,但不能指望智能手机能像平板电脑那样显示出来……

Get it?

明白了吗?

for more Information, check out this. I really think you just need to catch the concept....

要了解更多信息,请查看这里。我真的觉得你只需要抓住这个概念....

#4


6  

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.

历史上,Android应用程序的每个屏幕都是作为单独的活动来实现的。这给在屏幕之间传递信息带来了挑战,因为Android意图机制不允许在活动之间直接传递引用类型(例如对象)。相反,必须对对象进行序列化或提供全局可访问引用。

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://softwareengineering.stackexchange.com/questions/244771/why-use-android-fragments

https://softwareengineering.stackexchange.com/questions/244771/why-use-android-fragments

#5


0  

Fragment primary support more dynamic & Large UI Screen like Tablet.Because Tablet screen is much larger than normal Handset. There is more room to combine & Interchange UI Component.

碎片主要支持更动态和更大的UI屏幕,如平板电脑。因为平板电脑的屏幕比普通手机大得多。有更多的空间来合并和交换UI组件。

Fragment allow such design without the need for such complex change in the View hierarchy.

片断允许这样的设计,而不需要在视图层次结构中进行如此复杂的更改。

By divide activity layout in fragment, we become able to modify activity's appearance at runtime

通过在片段中划分活动布局,我们可以在运行时修改活动的外观