-
Notifications
You must be signed in to change notification settings - Fork 784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2019-10-10:请谈谈Fragment的生命周期? #161
Comments
1.fragment跟activity生命周期 |
1,onAttach:fragment 和 activity 关联时调用,且调用一次。在回调中可以将参数 content 转换为 Activity保存下来,避免后期频繁获取 activity。 2,onCreate:和 activity 的 onCreate 类似 3,onCreateView:准备绘制 fragment 界面时调用,返回值为根视图,注意使用 inflater 构建 View时 一定要将 attachToRoot 指明为 false。 4,onActivityCreated:activity 的onCreated 执行完时调用 5,onStart:可见时调用,前提是 activity 已经 started 6,onResume:交互式调用,前提是 activity 已经 resumed 7,onPause:不可交互时调用 8,onStop:不可见时调用 9,onDestroyView:移除 fragment 相关视图时调用 10,onDestroy:清除 fragmetn 状态是调用 11,onDetach:和 activity 解除关联时调用 从生命周期可以看出,他们是两两对应的,如 onAttach 和 onDetach , onCreate 和 onDestory ,onCreateView 和 onDestroyView等 ragment 在 ViewPager中的生命周期 ViewPager 有一个预加载机制,他会默认加载旁边的页面,也就是说在显示第一个页面的时候 旁边的页面已经加载完成了。这个可以设置,但不能为0,但是有些需求则不需要这个效果,这时候就可以使用懒加载了:懒加载的实现 1,当 ViewPager 可以左右滑动时,他左右两边的 fragment 已经加载完成,这就是预加载机制 2,当 fragment 不处于 ViewPager 左右两边时,就会执行 onPause,onStop,OnDestroyView 方法。 fragment 之间传递数据方法 1,使用 bundle,有些数据需要被序列化 2,接口回调 3,在创建的时候通过构造直接传入 4,使用 EventBus 等 单 Activity 多 fragment 的优点,fragment 的优缺点 fragment 比 activity 占用更少的资源,特别在中低端手机,fragment 的响应速度非常快,如丝般的顺滑,更容易控制每个场景的生命周期和状态 优缺点:非常流畅,节省资源,灵活性高,fragment 必须赖于acctivity,而且 fragment 的生命周期直接受所在的 activity 影响。 |
onAttach() ,片段和活动之间调用,调用一次,保存数据; 片段之间传递数据方法 2,接口 3,在创造的时候通过构造直接替代 4,使用EventBus等 单活动多fragment的优点,fragment的优缺点 优缺点:非常流畅,节省资源,规模高,碎片必须赖于活动性,而且碎片的生命周期直接受其所在的活动影响。 |
按照正常的执行顺序如下: |
Activity中的生命周期把Fragment添加到id为content的布局上
启动时 Fragment在ViewPager中的生命周期A->onCreate->onStart->onResume |
onAttach Activity关联Fragment时调用 |
No description provided.
The text was updated successfully, but these errors were encountered: