Android 片段与复合控件
为什么要使用Android 3.0片段而不是复合控件?人们可以创建一个视图继承器或复合控件一次并在任何地方使用它。
我读过 http://android-developers.blogspot.com/2011/ 02/android-30-fragments-api.html 但没有找到答案。
Why should Android 3.0 fragments be used instead of compound controls? One can create a View inheritor or compound control once and use it everywhere.
I've read http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html but did not find the answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不同之处在于片段有自己的生命周期(onPause、onCreate、onStart...)。通过具有生命周期,片段可以独立响应事件,通过 onSaveInstanceState 保存其状态,并返回(即,例如在来电后恢复或用户单击后退按钮时)。片段文档中总结了生命周期:
https://developer.android.com /guide/components/fragments.html#Lifecycle
您始终可以将片段或活动包装在复合视图周围,因此只需将片段视为复合视图的容器,从而为它们提供独立的生命周期。< /strong>
The difference is that fragments have a life cycle (onPause, onCreate, onStart...) of their own. By having a life cycle, fragments can respond independently to events, save their state through onSaveInstanceState, and be brought back (i.e. such as when resuming after an incoming call or when the user clicks the back button). The life cycle is summarized in the fragment documentation:
https://developer.android.com/guide/components/fragments.html#Lifecycle
You can always wrap a fragment or activity around a compound view, so just think of fragments as containers to your compound views that give them an independent life cycle.
原因是让相同的代码在平板电脑和手机上运行。这些设备有不同的布局注意事项,片段允许您考虑到这一点,并使您的应用程序表现不同,而无需重写任何代码。
The reason would be to have the same code work on tablets and phones. There are different layout considerations for these devices and Fragments allow you to take that into consideration and have your app behave differently without having to rewrite any code.