Fragment VS View

原文地址:http://stackoverflow.com/questions/8617696/what-is-the-benefit-of-using-fragments-in-android-rather-than-views


Coming from someone who actually tried to build a phone/tablet app using custom views, I don't think the accepted answer really explains the limitations of fragments and custom views.

Before I learned how fragments worked and about the compatibility jar, I attempted to create a phone/tablet app using custom views. Everything appeared to work across phones AND tablets, even switching from single panel to split panel. Where I ran into trouble was with the back button and life cycle. Since I was simply updating views manually...there was nothing keeping track of the history of views and their states. Therefore, the back button did not work as expected and it was difficult to recreate even the latest state during life cycle events, such as when rotating the app. To fix that, I had to wrap my custom views in fragments and use the FragmentManager so that the previous states would be saved and recreated.

Now, Fragments are not a silver bullet or always "better" than a custom view. For example, a huge drawback of fragments is that they cannot be nested. So if you've created a wonderful fragment that you now want to re-use it inside of a dialog, you will not be able to simply put it inside of a DialogFragment. Or if you've already created an app using 2 side-by-side panels and want to reuse a 3rd fragment inside one of the panels...it won't work either. You would have to switch to a 3-panel layout.

So, the main reason to use Fragments are for the backstack and lifecycle features. Otherwise, custom views are more light weight, simpler to implement, and have the advantage that they can be nested.




原文地址:http://stackoverflow.com/questions/9827072/why-use-fragments/14912608#14912608

The main reason is that fragments are more reusable than custom views.

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.

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.

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

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中的 Fragment 是一种 UI 组件,用于构建灵活的用户界面。Fragment 可以理解为 Activity 中的一个模块化部分,它可以包含自己的布局、生命周期和逻辑功能。而 Fragment View 则是 Fragment 中的视图,也可以理解为 Fragment 的界面部分。 在 Android 开发中,通过 Fragment View 可以实现更灵活、可复用的界面设计。一个 Fragment 可以包含多个 Fragment View,它们可以像 Activity 中的 View 那样处理用户交互事件、展示数据等。使用 Fragment View 可以将界面功能模块化,并且方便地在多个界面中复用。 在创建 Fragment View 时,我们可以通过 XML 布局文件进行定义,也可以通过代码动态地创建 View 对象。Fragment View 的生命周期与 Fragment 的生命周期密切相关,它们共享相同的生命周期方法,如 onCreateView()、onActivityCreated()、onDestroyView() 等。这样可以在 Fragment 的生命周期方法中对 Fragment View 进行初始化、数据更新等操作。 另外,通过 Fragment View 可以方便地与其他 Fragment 进行交互。我们可以在 Fragment View 中注册事件监听器,当用户与 Fragment View 交互时触发相应的事件,然后通过 Fragment 之间的通信机制来实现 Fragment 之间的数据传递、界面更新等功能。 总而言之,Android 中的 Fragment View 是一种可复用、可组合的界面组件,它可以帮助开发者快速构建灵活、模块化的用户界面。通过 Fragment View,我们可以更好地组织界面逻辑、实现界面之间的交互,并提高代码的可维护性和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值