android 为什么在activity.onStart之后调用fragment.onCreate而不是activity.onCreate

von4xj4u  于 2023-05-12  发布在  Android
关注(0)|答案(2)|浏览(173)

背景是包含片段的活动:

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")
    }
}
qmelpv7a

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
为了回答您的问题,FragmentonCreate()在其父ActivityonStart()之后被调用的原因是因为FragmentsActivity生命周期的一部分,它们的生命周期方法在相应的Activity生命周期方法之后被调用。
可以看到,FragmentonCreate()方法在其父ActivityonStart()方法之后被调用。这是因为onStart()是在Activity对用户可见时被调用的,只有在这一点之后,Fragment才能被添加到Activity中,并可以调用其onCreate()方法。
为了更好地理解它,这里有一张图片

pokxtpni

pokxtpni2#

ActivityonStart()方法表示该Activity现在将可见,但可能不在前台,并且可能被其他Activity遮挡。
当activity完成onStart()方法时,它表示activity可见,现在它可以执行片段或视图的任何附件
所以在**onStart()**方法fragments之后,方法将被调用。
我希望这对你的问题和你正在寻找的正确的问题有意义。

相关问题