Fragment VS View

本文探讨了在Android应用开发中使用片段与自定义视图的优劣对比,特别是片段在回退堆栈和生命周期方面的优势,以及它们各自的适用场景。通过实际案例,解释了片段如何解决自定义视图在回退按钮和生命周期事件处理上的问题,同时也阐述了片段不适用于某些特定情况的原因,如不能嵌套等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址: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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值