Issue
In android programming,
When we add fragment to specific layout,
we can use following code snippet
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?
Solution
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.
Answered By - Iulian Popescu
Answer Checked By - Gilberto Lyons (JavaFixing Admin)