Nem*_*emo 10 android android-fragments
在Fragment.getActivity() 的文档中,它说:Return the FragmentActivity this fragment is currently associated with. May return null if the fragment is associated with a Context instead.
但是片段怎么能不与活动相关联呢?我的意思是,我不是总是需要一个活动来在 Android 中显示任何内容吗?
片段不需要与FragmentActivity. 相反,它们实际上与 a 相关联FragmentController,它FragmentActivity碰巧代表您创建和管理,在创建、启动等托管活动时根据需要调用适当的调度方法。
这种间接级别是 Facebook 的聊天头(显示Window由a管理Service)如何能够重用FragmentaFragmentActivity使用的相同实例以及如何为 Android Automotive 构建导航应用程序(使用 aCarAppService显示汽车应用程序)允许您也重用碎片。
当然,如果您的Fragment 始终是从您的一项活动中创建的,那么,是的,您绝对可以假设它requireActivity()会FragmentActivity随时isAdded()返回true- 即在onAttach()和之间onDetach()。这确实包括在生命周期方法中,例如onViewCreated()或与您的 Fragment 的视图相关的任何内容(例如OnClickListener)。
| 归档时间: |
|
| 查看次数: |
235 次 |
| 最近记录: |