What is difference between 'new Fragment()' and 'Fragment.getInstance()' in Android?

∥☆過路亽.° 提交于 2021-02-07 19:49:49

问题


In android programming,

When we add fragment to specific layout,

we can use folloing codes

Fragment fragment = new SampleFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_layout, fragment);
fragmentTransaction.commit();

or

Fragment fragment = SampleFragment.getInstance();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_layout, fragment);
fragmentTransaction.commit();

I cannot understand what is difference between that fragment object define sentence. From some sources, when use 'Fragment.getInstance()' like singleton pattern, pass bundle data to fragment with 'getInstance(Bundle data)' method parameter.

Could you tell me what difference?


回答1:


First thing to note is that if the system destroyed your fragment and has to re-create it, it will call the constructor with no-args. This thing implies that you have to save your arguments somewhere to be used later (You can't create a constructor with args).

Now, let's get back to your question. For the moment, the 2 blocks of code are almost identical, but only for the example you provided. If you should pass some params to your fragment, things are a little different. The getInstance should add the needed arguments to your fragment, guaranteeing that they will be available in a future moment.

Personally, I use the getInstance/newInstance (you may find variation of it, right now, creating a template fragment in Android Studio use the newInstance) method passing the parameters that I need in that fragment. For example, if I need two strings in that fragment, I will pass them to getInstance method and save them in the fragment arguments to make sure that they will be available if the fragment is re-created.

public static MyFragment getInstance(String param1, String param2) {
    MyFragment fragment = new MyFragment();
    Bundle args = new Bundle();
    args.putString(KEY_ONE, param1);
    args.putString(KET_TWO, param2);
    fragment.setArguments(args);
    return fragment;
}

Of course, for this method you can pass a Bundle, but I think that is a little bit clearer in this way, specifying each parameter the fragment will use.

That being said, if you want to create an equivalent of the above block you should use something like this:

MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
// set your arguments here
fragment.setArguments(args);
// and from here use your code as you did

To conclude, using getInstance is used to stop the code repetition (note that if you should create the fragment 10 times, you should copy the above code 10 times) and, if you create the fragment correctly (second block of code) you don't really have to use the getInstance method, but it's recommended.




回答2:


getInstance() for Fragment instantiation is a familiar design pattern, which encapsulate the creation of the fragment and its arguments. It means basically that the Fragment is responsible on creating its own instance and should be cleaner and safer than calling only new Fragment(), since you can pass additional data/bundle and "force" the user to use this method. Notice that you are still calling new Fragment() in the getInstance() method, it does not replace it.

public static SomeFragment newInstance(int a, boolean b) {
SomeFragment someFragment = new SomeFragment();

Bundle args = new Bundle();
args.putInt("a", a);
args.putBoolean("b",b);
.....
someFragment.setArguments(args);

return someFragment;
}

that way you will have only one place you would create the parameters bundle and not every time you want to instantiate the fragment.



来源:https://stackoverflow.com/questions/41158396/what-is-difference-between-new-fragment-and-fragment-getinstance-in-andr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!