DialogFragment
class TestDialog : DialogFragment() { lateinit var binding: MajlisDialogAccountCreateBinding override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { onDialogConfig() logD(----) { onCreateView dialog${dialog},window${dialog?.window} } binding MajlisDialogAccountCreateBinding.inflate(inflater, container, false) return binding.root } override fun onStart() { logD(----) { onStart-start dialog${dialog},window${dialog?.window} } super.onStart() logD(----) { onStart-end dialog${dialog},window${dialog?.window} } // onDialogConfig() } fun onDialogConfig() { dialog?.apply { window?.apply { setBackgroundDrawable(ColorDrawable(Color.RED)) attributes.width WindowManager.LayoutParams.MATCH_PARENT attributes.height WindowManager.LayoutParams.WRAP_CONTENT attributes attributes } setCanceledOnTouchOutside(isCancelable) setCancelable(isCancelable) } } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) logD(----) { onViewCreated dialog${dialog},window${dialog?.window} } binding.ivClear.run { visible() setOnClickListener { binding.etAccount.setText() toast { ssss } } } } init { logD(----) { init } } override fun onAttach(context: Context) { super.onAttach(context) logD(----) { onAttach dialog${dialog},window${dialog?.window} } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) logD(----) { onCreate dialog${dialog},window${dialog?.window} } } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val a super.onCreateDialog(savedInstanceState) logD(----) { onCreateDialog dialog${dialog},window${dialog?.window} } return a } override fun onResume() { super.onResume() logD(----) { onResume dialog${dialog},window${dialog?.window} } } override fun onPause() { super.onPause() logD(----) { onPause dialog${dialog},window${dialog?.window} } } override fun onStop() { super.onStop() logD(----) { onStop dialog${dialog},window${dialog?.window} } } override fun onDestroyView() { super.onDestroyView() logD(----) { onDestroyView dialog${dialog},window${dialog?.window} } } override fun onDestroy() { super.onDestroy() logD(----) { onDestroy dialog${dialog},window${dialog?.window} } } override fun onDetach() { super.onDetach() logD(----) { onDetach dialog${dialog},window${dialog?.window} } } }生命周期1、TestDialog().show(supportFragmentManager, test)onAttach()onCreate()onCreateDialog() //这里创建dialogonCreateView() // 如果重写了onViewCreated() // 如果有 ViewonStart()onResume()2、关闭 DialogonPause()onStop()onDestroyView()onDestroy()onDetach()问题1、如果只是TestDialog()会走哪些方法只会走构造方法2、fragementDialog和他里面的dialog有什么关系1. 先给结论非常重要DialogFragment里面的关系是DialogFragment (Fragment) ↓ 持有 dialog (android.app.Dialog) ↓ 包含 window (Window)也就是说Fragment 是容器Dialog 是真正弹窗对象2. 三者关系图核心理解DialogFragment │ ├── FragmentManager 管理生命周期 │ ├── dialog : Dialog ← 真正的弹窗 │ │ │ └── window ← 真正显示在屏幕上的窗口 │ └── View (onCreateView 可选)