Brief introduction
Fragment is often used in our usual development, and when we create a Fragment object it is usually implemented by the new Fragment () constructor, and if we are passing parameters we will generally overload the construction method, such as New Fragment ( Parameter p).
However, this is not advocated, if you overload the constructor, the system will have a warning, as follows:
Avoid non-defaultindefaultconstructor plus Fragment#setArguments(Bundle) instead
The caveat is to try to avoid using constructors that are not the default (that is, our overloaded constructors): By using the default constructor plus fragment.setarguments (Bundle) instead.
Let's take a look at both of these ways: overloaded constructors:
publicclass MyFragment extends Fragment { publicMyFragment(MyParameter p){ //将参数保存起来 }}
If you are creating fragment, use MyFragment mf = new MyFragment(parameter)
the
To pass parameters.
by Fragment.setarguments (Bundle)
publicclass MyFragment extends Fragment { publicstaticnewInstance(int someInt) { new MyFragment(); new Bundle(); args.putInt("someInt", someInt); myFragment.setArguments(args); return myFragment; }}
This method is used MyFragment mf = MyFragment.newInstance(paramter)
to pass parameters when creating fragment.
Analysis
At first glance, there seems to be no essential difference between the two methods, but in fact method one (overloaded constructors) has a hidden danger.
according to the Android documentation, when a fragment is re-created, the system calls the default constructor in fragment again. Note here: is the default constructor .
It's more straightforward to say that when you carefully create a fragment with important parameters, once you've created your fragment, you'll have to recreate it for whatever reason (toggle screen). ——-regret to tell you that the parameters you passed were missing because the default constructor was called when recreate your fragment.
Contrast
Instead, use the system's recommended fragment.setarguments (Bundle) to pass the parameters. Can effectively avoid this problem, when your fragment destroyed, the bundle will be saved, when the re-creation will check whether the bundle is NULL, if not NULL, will use the bundle as a parameter to recreate the fragment.
Questions:
when the fragment is rebuilt, how do you get the previous parameters?
Take the code above as an example: we can rewrite the Fragment OnCreate () method.
getArguments().getInt("someInt"0);
When the fragment is recreated, the OnCreate method is called, then the previous parameters can be obtained and then it can be used.
How does the bundle save the parameters and then re-use them when they are rebuilt?
This question can be a part of this blog.
Attention:
Setarguments can only be called before the Fragment are attached to the Activity.
The Setarguments method must be called before fragment is associated with the activity.
This sentence can be understood in this way, the use of the Setargument method must be used before fragmenttransaction commit.
See: Connection.
Reference 1:
Reference 2
Reference 3
The original intention of fragment.setarguments ().