Android DialogFragment getDialog() 和 getView() 返回 null
Android DialogFragment getDialog() and getView() returning null
我有一个 DialogFragment,我需要从 Fragment 中显示它。这是我的 DialogFragment:
class MyDialogFragment : DialogFragment() {
companion object {
@JvmStatic
internal fun newInstance(): MyDialogFragment = MyDialogFragment()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.my_dialog_fragment container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<MaterialButton>(R.id.cancel_button)?.setOnClickListener { dismiss() }
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(requireActivity())
val dialogView = requireActivity().layoutInflater.inflate(R.layout.my_dialog_fragment, null)
builder.setView(dialogView)
dialogView.findViewById<Button>(R.id.cancel_button).setOnClickListener { dismiss() }
return builder.create()
}
fun setMyAction(action: () -> Unit) {
view?.findViewById<MaterialButton>(R.id.proceed_button)?.setOnClickListener { action() }
}
}
我从片段中调用它的方式是这样的:
btnSubmit.setOnClickListener(view -> {
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(getChildFragmentManager(), MyDialogFragment.class.getSimpleName());
myDialogFragment.setMyAction(this::action);
});
我这里的问题是无法从调用片段调用 setMyAction
并调用 action()
,因为在对话框中,view
是 null
就像我尝试用 dialog()
获取它一样。这里的第二个问题是:为什么 onCreateView
从未被调用,就像 onViewCreated
?
提前致谢!
The problem I have here is that it is impossible to call setMyAction from the calling fragment and call the action(), since in the dialog, view is null just like if I try to get it with dialog().
您现在遇到的问题是 show()
是异步的。该对话框将在稍后创建,而不是在 show()
返回时创建。
您尚未遇到但将会遇到的问题是,在配置更改时,默认情况下将重新创建您的 activity 及其片段,而您的 OnClickListener
将会丢失。
与其推送事件处理程序,不如让 DialogFragment
公开结果(例如,通过共享 ViewModel
)。
A second problem here is: why is the onCreateView never called, just like onViewCreated?
您推翻了 onCreateDialog()
。您不能同时使用 onCreateDialog()
和 onCreateView()
/onViewCreated()
。选择一个并使用它。
我有一个 DialogFragment,我需要从 Fragment 中显示它。这是我的 DialogFragment:
class MyDialogFragment : DialogFragment() {
companion object {
@JvmStatic
internal fun newInstance(): MyDialogFragment = MyDialogFragment()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.my_dialog_fragment container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<MaterialButton>(R.id.cancel_button)?.setOnClickListener { dismiss() }
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(requireActivity())
val dialogView = requireActivity().layoutInflater.inflate(R.layout.my_dialog_fragment, null)
builder.setView(dialogView)
dialogView.findViewById<Button>(R.id.cancel_button).setOnClickListener { dismiss() }
return builder.create()
}
fun setMyAction(action: () -> Unit) {
view?.findViewById<MaterialButton>(R.id.proceed_button)?.setOnClickListener { action() }
}
}
我从片段中调用它的方式是这样的:
btnSubmit.setOnClickListener(view -> {
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(getChildFragmentManager(), MyDialogFragment.class.getSimpleName());
myDialogFragment.setMyAction(this::action);
});
我这里的问题是无法从调用片段调用 setMyAction
并调用 action()
,因为在对话框中,view
是 null
就像我尝试用 dialog()
获取它一样。这里的第二个问题是:为什么 onCreateView
从未被调用,就像 onViewCreated
?
提前致谢!
The problem I have here is that it is impossible to call setMyAction from the calling fragment and call the action(), since in the dialog, view is null just like if I try to get it with dialog().
您现在遇到的问题是 show()
是异步的。该对话框将在稍后创建,而不是在 show()
返回时创建。
您尚未遇到但将会遇到的问题是,在配置更改时,默认情况下将重新创建您的 activity 及其片段,而您的 OnClickListener
将会丢失。
与其推送事件处理程序,不如让 DialogFragment
公开结果(例如,通过共享 ViewModel
)。
A second problem here is: why is the onCreateView never called, just like onViewCreated?
您推翻了 onCreateDialog()
。您不能同时使用 onCreateDialog()
和 onCreateView()
/onViewCreated()
。选择一个并使用它。