我已经看到了在应用程序中实例化新片段的两种一般做法:
Fragment newFragment = new MyFragment();
和
Fragment newFragment = MyFragment.newInstance();
The second option makes use of a static method newInstance()
和 generally contains the following method.
public static Fragment newInstance()
{
MyFragment myFragment = new MyFragment();
return myFragment;
}
起初,我认为主要的好处是我可以重载newInstance()方法,以便在创建片段的新实例时提供灵活性-但我也可以通过为片段创建重载的构造函数来实现这一点.
我错过什么了吗?
一种方法比另一种方法有什么好处?还是仅仅是良好的实践?