何时在片段中使用onActivitycreate()和onCreate() [英] When to use onActivitycreate() and onCreate() in fragments

查看:45
本文介绍了何时在片段中使用onActivitycreate()和onCreate()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始处理片段.在开始之前,我想知道是否应该使用什么时间的疑问.onActivitycreate()和onCreate().我进行了许多研究,但找不到任何相关的答案.请帮忙.

I have started working on fragments.Before starting i wanna clear a doubt whether when to use onActivitycreate() and onCreate().I did many research but cound not find any relevent answer.Please help.

推荐答案

onActivitycreate():

onActivitycreate()在创建片段的活动并实例化此片段的视图层次结构时调用.一旦这些部分就位,就可以用来进行最终初始化,例如检索视图或还原状态.这对于使用setRetainInstance(boolean)保留其实例的片段也很有用,因为此回调告诉片段何时将其与新的活动实例完全关联.在onCreateView(LayoutInflater,ViewGroup,Bundle)之后和onViewStateRestored(Bundle)之前调用.

onActivitycreate() Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onViewStateRestored(Bundle).

onCreate():

系统在创建片段时调用此方法.在实现过程中,您应该初始化要在暂停或停止然后恢复的片段时要保留的片段的基本组件.

The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

阅读此文档以了解更多详细信息: 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆