Fragment. setArguments ., Setarguments
Introduction
Fragment is often used during normal development, and we usually use the new Fragment () constructor to create a Fragment object, if you want to pass parameters, we will generally overload the constructor, for example, new Fragment (Parameter p ).
However, this approach is not recommended. If you reload the constructor, the system will have a warning, as shown below:
Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead
This warning means to avoid using non-default constructor (that is, our overloaded constructor) as much as possible: replace it by using the default constructor with Fragment. setArguments (Bundle.
Next, let's take a look at the two methods: the overloaded constructor:
Public class MyFragment extends Fragment {public MyFragment (MyParameter p) {// Save the parameter }}
If you useMyFragment mf = new MyFragment(parameter)
To pass parameters.
Use Fragment. setArguments (Bundle)
public class MyFragment extends Fragment { public static MyFragment newInstance(int someInt) { MyFragment myFragment = new MyFragment(); Bundle args = new Bundle(); args.putInt("someInt", someInt); myFragment.setArguments(args); return myFragment; }}
This method is used when creating Fragment.MyFragment mf = MyFragment.newInstance(paramter)To pass parameters.
Analysis
At first glance, there seems to be no essential difference between the two methods, but actually method 1 (overload constructor) has a hidden risk.
According to the Android documentation, when a fragment is re-created, the system will call the default constructor in Fragment again.Note: YesDefault constructor.
This statement is more straightforward: After you carefully create a Fragment with an important parameter, your Fragment will be re-created due to what causes (landscape switching. --- Sorry to tell you that all the previously passed parameters are missing, because when you recreate your Fragment, the call isDefault constructor.
Comparison
Instead, use the Fragment. setArguments (Bundle) recommended by the system to pass parameters. This problem can be effectively avoided. When your Fragment is destroyed, the Bundle will be saved and the Bundle will be checked if it is null when it is re-created, if it is not null, bundle is used as the parameter to recreate fragment.
Question:
When fragment is rebuilt, how can we get the previous parameters?
The above code is used as an example: We can override the onCreate () method of fragment.
getArguments().getInt("someInt", 0);
When fragment is created again, call the onCreate method to obtain the previous parameters, and then use them as much as possible.
How does bundle Save the parameters and reuse them during reconstruction?
To solve this problem, go to this blog.
Note:
SetArguments can only be called before the Fragment is attached to the Activity.
The setArguments method must be called before the Fragment is associated with the Activity.
It can be understood that the setArgument method must be used before the commit of FragmentTransaction.
For details, see connection.
Reference 1:
References 2
Reference 3