Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
156 views
Welcome To Ask or Share your Answers For Others

1 Answer

If Android decides to recreate your Fragment later, it's going to call the no-argument constructor of your fragment.

(如果Android决定稍后再创建片段,它将调用片段的无参数构造函数。)

So overloading the constructor is not a solution.

(因此,重载构造函数不是解决方案。)

With that being said, the way to pass stuff to your Fragment so that they are available after a Fragment is recreated by Android is to pass a bundle to the setArguments method.

(话虽如此,将内容传递给Fragment以便在Android重新创建Fragment后可用的方法是将包传递给setArguments方法。)

So, for example, if we wanted to pass an integer to the fragment we would use something like:

(因此,例如,如果我们想将整数传递给片段,我们将使用类似以下内容:)

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

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

    return myFragment;
}

And later in the Fragment onCreate() you can access that integer by using:

(然后在Fragment onCreate()您可以使用以下方式访问该整数:)

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

This Bundle will be available even if the Fragment is somehow recreated by Android.

(即使Fragment是由Android重新创建的,此Bundle仍然可用。)

Also note: setArguments can only be called before the Fragment is attached to the Activity.

(另请注意:只能在将Fragment附加到Activity之前调用setArguments 。)

This approach is also documented in the android developer reference: https://developer.android.com/reference/android/app/Fragment.html

(android开发人员参考中也记录了这种方法: https : //developer.android.com/reference/android/app/Fragment.html)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...