I have seen two general practices to instantiate a new Fragment in an application:
(我已经看到了两种在应用程序中实例化新Fragment的常规做法:)
Fragment newFragment = new MyFragment();
and
(和)
Fragment newFragment = MyFragment.newInstance();
The second option makes use of a static method newInstance()
and generally contains the following method.
(第二个选项使用静态方法newInstance()
并且通常包含以下方法。)
public static Fragment newInstance()
{
MyFragment myFragment = new MyFragment();
return myFragment;
}
At first, I thought the main benefit was the fact that I could overload the newInstance() method to give flexibility when creating new instances of a Fragment - but I could also do this by creating an overloaded constructor for the Fragment.
(最初,我认为主要的好处是可以在创建Fragment的新实例时重载newInstance()方法以提供灵活性-但我也可以通过为Fragment创建重载的构造函数来做到这一点。)
Did I miss something?
(我错过了什么?)
What are the benefits of one approach over the other?
(一种方法比另一种方法有什么好处?)
Or is it just good practice?(还是只是好的做法?)
ask by Graham Smith translate from so