Android fragments vs compound controls

强颜欢笑 提交于 2020-01-11 17:41:55

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6214780/android-fragments-vs-compound-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!