背景是包含片段的活动:
override fun onCreate(savedInstanceState: Bundle?) {
Log.d("===Activity: ","onCreate")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState == null) {
supportFragmentManager.commit {
add(R.id.main_cv, MainFragment())
setReorderingAllowed(true)
addToBackStack(null)
}
}
}
然后我使用一些日志来观察这些生命周期回调的顺序,结果如下:
===Activity:: onCreate
===Activity:: onStart
===Fragment:: onAttach
===Fragment:: onCreate
===Fragment:: onCreateView
===Fragment:: onViewCreated
===Fragment:: onStart
我不知道为什么fragment.onCreate在activity.onStart之后调用而不是activity.onCreate。活动:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Log.d("===Activity: ","onCreate")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState == null) {
supportFragmentManager.commit {
add(R.id.main_cv, MainFragment())
setReorderingAllowed(true)
addToBackStack(null)
}
}
}
override fun onStart() {
Log.d("===Activity: ","onStart")
super.onStart()
}
override fun onResume() {
Log.d("===Activity: ","onResume")
super.onResume()
}
override fun onPause() {
Log.d("===Activity: ","onPause")
super.onPause()
}
override fun onStop() {
Log.d("===Activity: ","onStop")
super.onStop()
}
override fun onDestroy() {
Log.d("===Activity: ","onDestroy")
super.onDestroy()
}
}
片段:
class MainFragment : Fragment() {
override fun onAttach(context: Context) {
Log.d("===Fragment: ","onAttach")
super.onAttach(context)
}
override fun onCreate(savedInstanceState: Bundle?) {
Log.d("===Fragment: ","onCreate")
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
Log.d("===Fragment: ","onCreateView")
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Log.d("===Fragment: ","onViewCreated")
super.onViewCreated(view, savedInstanceState)
}
override fun onStart() {
Log.d("===Fragment: ","onStart")
super.onStart()
}
override fun onResume() {
Log.d("===Fragment: ","onResume")
super.onResume()
}
override fun onPause() {
super.onPause()
Log.d("===Fragment: ","onPause")
}
override fun onStop() {
super.onStop()
Log.d("===Fragment: ","onStop")
}
override fun onDestroyView() {
super.onDestroyView()
Log.d("===Fragment: ","onDestroyView")
}
override fun onDestroy() {
super.onDestroy()
Log.d("===Fragment: ","onDestroy")
}
}
2条答案
按热度按时间qmelpv7a1#
创建
Activity
时,它会经历以下阶段:1.
onCreate()
1.
onStart()
1.
onResume()
当
Fragment
被添加到Activity
时,它会经历以下阶段:1.
onAttach()
1.
onCreate()
1.
onCreateView()
1.
onViewCreated()
1.
onActivityCreated()
1.
onStart()
1.
onResume()
为什么在activity.onStart之后调用fragment.onCreate而不是activity.onCreate
为了回答您的问题,
Fragment
的onCreate()
在其父Activity
的onStart()
之后被调用的原因是因为Fragments
是Activity
生命周期的一部分,它们的生命周期方法在相应的Activity
生命周期方法之后被调用。可以看到,
Fragment
的onCreate()
方法在其父Activity
的onStart()
方法之后被调用。这是因为onStart()
是在Activity
对用户可见时被调用的,只有在这一点之后,Fragment
才能被添加到Activity
中,并可以调用其onCreate()
方法。为了更好地理解它,这里有一张图片
pokxtpni2#
ActivityonStart()方法表示该Activity现在将可见,但可能不在前台,并且可能被其他Activity遮挡。
当activity完成onStart()方法时,它表示activity可见,现在它可以执行片段或视图的任何附件。
所以在**onStart()**方法fragments之后,方法将被调用。
我希望这对你的问题和你正在寻找的正确的问题有意义。