您的位置:首页 > 移动开发 > Android开发

Android Fragment 是什么?

2016-07-12 16:52 302 查看
Android Fragment是Android3.0版本带的特性,其目的是为了解决Android开源而造成的严重碎片化。Fragment有什么用呢?可以先看一下这个类的官方解释:

A Fragment is a piece of an application’s user interface or behavior that can be placed in an Activity. Interaction with fragments is done through FragmentManager, which can be obtained via Activity.getFragmentManager() and Fragment.getFragmentManager().

The Fragment class can be used many ways to achieve a wide variety of results. In its core, it represents a particular operation or interface that is running within a larger Activity. A Fragment is closely tied to the Activity it is in, and can not be used apart from one. Though Fragment defines its own lifecycle, that lifecycle is dependent on its activity: if the activity is stopped, no fragments inside of it can be started; when the activity is destroyed, all fragments will be destroyed.

All subclasses of Fragment must include a public no-argument constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the no-argument constructor is not available, a runtime exception will occur in some cases during state restore.

可以看出,Fragment是放在activity当中的一小段应用交互接口。他和Activity是紧密联系的,如果Actvity被销毁,那么Fragment也会被销毁。

Fragment的用法在引用的第三段中有说明。具体的例子我这里就不写了,网上有很多,如果有需要可以自己去搜索。

总结:

1,Frament的出现是为了解决Android碎片化

2,Fragment本身可以理解为GroupView,目的是完成一小段交互

3,Fragment可以被动态加载,销毁

4,有自己的生命周期
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: