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() //这里创建dialog
onCreateView() // 如果重写了
onViewCreated() // 如果有 View
onStart()
onResume()
2、关闭 Dialog:
onPause()
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 可选)