通过构造函数传递数据和通过 DialogFragment 的参数传递数据有什么区别?
What is the difference between passing data through constructor and arguments of DialogFragment?
我有一个简单的BottomSheetDialogFragment
:
class MyBottomSheetDialog : BottomSheetDialogFragment() {
companion object {
private const val SOME_KEY = "some_key"
fun newInstance(something: Boolean): MyBottomSheetDialog {
return MyBottomSheetDialog().apply {
arguments = bundleOf(SOME_KEY to something)
}
}
}
...
...
}
然后我显示:
MyBottomSheetDialog.newInstance(false).show(childFragmentManager, "my_dialog")
这种仅使用构造函数参数并显示如下对话框的典型方法的优势是什么:
class MyBottomSheetDialog(private val something: Boolean) : BottomSheetDialogFragment() {
...
...
}
MyBottomSheetDialog(false).show(childFragmentManager, "my_dialog")
简单的事情是,如果你的 class 经历了配置更改,那么系统会寻找默认构造函数来重新创建 class 即没有参数,你将得到异常。
我有一个简单的BottomSheetDialogFragment
:
class MyBottomSheetDialog : BottomSheetDialogFragment() {
companion object {
private const val SOME_KEY = "some_key"
fun newInstance(something: Boolean): MyBottomSheetDialog {
return MyBottomSheetDialog().apply {
arguments = bundleOf(SOME_KEY to something)
}
}
}
...
...
}
然后我显示:
MyBottomSheetDialog.newInstance(false).show(childFragmentManager, "my_dialog")
这种仅使用构造函数参数并显示如下对话框的典型方法的优势是什么:
class MyBottomSheetDialog(private val something: Boolean) : BottomSheetDialogFragment() {
...
...
}
MyBottomSheetDialog(false).show(childFragmentManager, "my_dialog")
简单的事情是,如果你的 class 经历了配置更改,那么系统会寻找默认构造函数来重新创建 class 即没有参数,你将得到异常。