为什么要使用的newInstance的DialogFragment而不是构造方法? [英] Why use newInstance for DialogFragment instead of the constructor?
问题描述
纵观<一href="http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog">documentation DialogFragment
中,人们看到的静态的newInstance
方法来初始化一个新的警告对话框片段。我的问题是,为什么不使用构造函数的话,像这样的:
Looking at the documentation of DialogFragment
, one sees the static newInstance
method to initialize a new alert dialog fragment. My question is, why not use a constructor to do so, like this:
public MyAlertDialogFragment(int title) {
Bundle args = new Bundle();
args.putInt("title", title);
setArguments(args);
}
这不是完全一样的还是它的不同不知?什么是最好的方法和为什么
推荐答案
如果你超载与 MyAlertDialogFragment(INT标题)构造
,Android系统仍然可以调用默认 MyAlertDialogFragment()
的构造函数,如果片段
需要重新创建和参数,然后不通过。
If you overload the constructor with MyAlertDialogFragment(int title)
, the Android system may still call the default MyAlertDialogFragment()
constructor if the Fragment
needs to be recreated and the parameter is then not passed.
这篇关于为什么要使用的newInstance的DialogFragment而不是构造方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!