Default constructor. Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().
Applications should generally not implement a constructor. The first place application code an run where the fragment is ready to be used is inonAttach(Activity), the point where the fragment is actually associated with its activity. Some applications may also want to implementonInflate(Activity, AttributeSet, Bundle) to retrieve attributes from a layout resource, though should take care here because this happens for the fragment is attached to its activity.
默认的构造函数,每个Fragment必须有一个空的构造函数,这可以让它在Activity中恢复时实例化。强烈建议Fragment的子类不要拥有其他有参数的构造函数,因为这些构造器不会在Fragment重新实例化时被调用;取而代之, 调用者能以setArguments(Bundle)提供arguments,然后被Fragment以getArguments()取回。
应用程序一般不要实现构造器。一个可以使用的Fragment的开始代码是onAttach(Activity),事实上这是Fragment和Activity交接的地方。有些应用程序也会去实现onInflate(Activity, AttributeSet, Bundle)方法从layout资源中获得属性,但是在这里应该特别小心,因为这发生在Fragment附加上它的Activity时候。
分享到:
相关推荐
androidx-fragment-1.1.0.aar
Android 程序技术 本节课程内容:Fragment的介绍 Fragment Fragment(碎片)是一种可以嵌入在Activity中的UI片段,与Activity非常相似,不仅包含布局,同时也具有自己的生命周期 Fragment不能独立存在必须嵌入到...
这是Pro Android学习Fragment部分的例子源代码。
android:Fragment和Activity之间的通讯分享.pdf
上集给大家带来了android的面子activity的介绍,不知各位看官掌握怎样了,这集隆重登场的Fragment其实和Activity有着千丝万缕的联系。Fragment让界面的创建更加灵活和多变,且复用性得到进一步的提升。疯兄认为,一...
问题解决:Fragment not attached to Activity的相关代码,更多详细内容请参考:http://blog.csdn.net/u012939909/article/details/53355836
Android Studio动态加载Fragment和获取实例的方法
我的微博《页面架构二:Fragment+LinearLayout》对应的完整源代码的module
android:TabHost+Fragment实现Tab切换
以前做这种新闻标签页都是用LocalActivityManager。当把api升级到最新后发现这个LocalActivityManager已经被废弃了,取而代之的是FragmentManager,经过一下午的研究终于用FragmentManager实现了新闻滑动标签页的...
我的微博《页面框架一:Fragment + RadioGroup》对应的完整源代码
Fragment的四种跳转方式: 1、从同一个Activiy的一个Fragment跳转到另外一个Fragment 2、从一个Activity的Fragment跳转到另外一个Activity 3、从一个Activity跳转到另外一个Activity的Fragment上 4、从一个Activity...
Android DataBinding ViewPage + Fragment 封装
Android解决多个Fragment切换时布局重新实例化问题,并保存当前fragment页面,系统回收后还原之前fragment页面,内附源码及apk,博客:http://blog.csdn.net/fan7983377/article/details/51889269
Android_activity向fragment传值
Android Fragment切换动画
网上关于androidx的fragment懒加载文章已经有很多,各有侧重。几乎都点到了sexMaxLifecycle和修改FragmentPagerAdapter。很少看到经过实践的文章,谨以此文,更加详尽的把实践后的结果记录下来,赠予有缘人。 一、...
Android中在xml中静态添加Fragment 在activity_main.xml中添加LeftFragment和RightFragment
Android Activity动态加载FragmentDemo(开发工具Android Studio3.1.4)