我已经看到了在应用程序中实例化一个新Fragment的两个通用实践:

Fragment newFragment = new MyFragment();

and

Fragment newFragment = MyFragment.newInstance();

第二个选项使用静态方法newInstance(),通常包含以下方法。

public static Fragment newInstance() 
{
    MyFragment myFragment = new MyFragment();
    return myFragment;
}

起初,我认为主要的好处是,我可以重载newInstance()方法,以便在创建新的Fragment实例时提供灵活性——但我也可以通过为Fragment创建重载构造函数来实现这一点。

我错过什么了吗?

一种方法相对于另一种方法有什么好处?还是说这只是一个很好的练习?


当前回答

使用newInstance()的唯一好处是:

You will have a single place where all the arguments used by the fragment could be bundled up and you don't have to write the code below everytime you instantiate a fragment. Bundle args = new Bundle(); args.putInt("someInt", someInt); args.putString("someString", someString); // Put any other arguments myFragment.setArguments(args); Its a good way to tell other classes what arguments it expects to work faithfully(though you should be able to handle cases if no arguments are bundled in the fragment instance).

因此,我认为使用静态newInstance()来实例化一个片段是一个很好的实践。

其他回答

实例化片段的最好方法是使用默认的片段。实例化方法或创建工厂方法来实例化片段 注意:总是在片段中创建一个空构造函数,而在恢复片段内存时会抛出运行时异常。

我相信我有一个更简单的解决方案。

public class MyFragment extends Fragment{

   private String mTitle;
   private List<MyObject> mObjects;

   public static MyFragment newInstance(String title, List<MyObject> objects)
   MyFragment myFrag = new MyFragment();
   myFrag.mTitle = title;
   myFrag.mObjects = objects;
   return myFrag;
   }

setArguments()没有用。这只会带来混乱。

public class MyFragment extends Fragment {

    public String mTitle;
    public String mInitialTitle;

    public static MyFragment newInstance(String param1) {
        MyFragment f = new MyFragment();
        f.mInitialTitle = param1;
        f.mTitle = param1;
        return f;
    }

    @Override
    public void onSaveInstanceState(Bundle state) {
        state.putString("mInitialTitle", mInitialTitle);
        state.putString("mTitle", mTitle);
        super.onSaveInstanceState(state);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
        if (state != null) {
            mInitialTitle = state.getString("mInitialTitle");
            mTitle = state.getString("mTitle");
        } 
        ...
    }
}

我不同意yydi的回答:

如果Android决定稍后重新创建Fragment,它会调用 片段的无参数构造函数。重载 构造函数不是解决方案。

我认为这是一个很好的解决方案,这正是它被Java核心语言开发的原因。

确实,Android系统可以破坏和重建你的碎片。所以你可以这样做:

public MyFragment() {
//  An empty constructor for Android System to use, otherwise exception may occur.
}

public MyFragment(int someInt) {
    Bundle args = new Bundle();
    args.putInt("someInt", someInt);
    setArguments(args);
}

它将允许您稍后从getArguments()中提取someInt,即使该Fragment已被系统重新创建。这是比静态构造函数更优雅的解决方案。

在我看来,静态构造函数是无用的,不应该使用。此外,如果将来你想扩展这个Fragment并向构造函数添加更多功能,它们也会限制你。使用静态构造函数你不能这样做。

更新:

Android增加了标记所有非默认构造函数错误的检查。 由于上面提到的原因,我建议禁用它。

还有另一种方法:

Fragment.instantiate(context, MyFragment.class.getName(), myBundle)