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

Fragment newFragment = new MyFragment();

and

Fragment newFragment = MyFragment.newInstance();

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

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

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

我错过什么了吗?

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


当前回答

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

话虽如此,传递东西到你的碎片,使他们可用后,一个碎片被Android重新创建是传递一个捆绑到setArguments方法。

因此,例如,如果我们想要传递一个整数到片段,我们将使用如下方式:

public static MyFragment newInstance(int someInt) {
    MyFragment myFragment = new MyFragment();

    Bundle args = new Bundle();
    args.putInt("someInt", someInt);
    myFragment.setArguments(args);

    return myFragment;
}

稍后在Fragment onCreate()中,你可以使用:

getArguments().getInt("someInt", 0);

即使Fragment被Android以某种方式重新创建,这个Bundle也将可用。

还要注意:setArguments只能在Fragment被附加到Activity之前被调用。

这种方法也记录在android开发者参考:https://developer.android.com/reference/android/app/Fragment.html

其他回答

理想情况下,我们不应该在片段构造函数中传递任何东西,片段构造函数应该是空的或默认的。 现在第二个问题是,如果我们想传递接口变量或参数 我们应该使用Bundle来传递数据。 对于Interface,我们可以在bundle中putParceble,并使该接口实现parceble 如果可能的话,我们可以在活动和片段中实现该接口,我们可以在OnAttach中初始化监听器,在那里我们有上下文[(上下文)监听器]。

因此,在配置更改期间(例如字体更改),Activity重新创建侦听器不会去非初始化,我们可以避免空指针异常。

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

话虽如此,传递东西到你的碎片,使他们可用后,一个碎片被Android重新创建是传递一个捆绑到setArguments方法。

因此,例如,如果我们想要传递一个整数到片段,我们将使用如下方式:

public static MyFragment newInstance(int someInt) {
    MyFragment myFragment = new MyFragment();

    Bundle args = new Bundle();
    args.putInt("someInt", someInt);
    myFragment.setArguments(args);

    return myFragment;
}

稍后在Fragment onCreate()中,你可以使用:

getArguments().getInt("someInt", 0);

即使Fragment被Android以某种方式重新创建,这个Bundle也将可用。

还要注意:setArguments只能在Fragment被附加到Activity之前被调用。

这种方法也记录在android开发者参考:https://developer.android.com/reference/android/app/Fragment.html

使用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()来实例化一个片段是一个很好的实践。

一些kotlin代码:

companion object {
    fun newInstance(first: String, second: String) : SampleFragment {
        return SampleFragment().apply {
            arguments = Bundle().apply {
                putString("firstString", first)
                putString("secondString", second)
            }
        }
    }
}

你可以得到这样的论点:

val first: String by lazy { arguments?.getString("firstString") ?: "default"}
val second: String by lazy { arguments?.getString("secondString") ?: "default"}

还有另一种方法:

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