What is the difference between getArguments
Bundle and Bundle received in the onCreate
?
onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.
onCreate() is called to do initial creation of the fragment. onCreateView() is called by Android once the Fragment should inflate a view. onViewCreated() is called after onCreateView() and ensures that the fragment's root view is non-null .
TL;DR:
Fragment.getArguments() is for initially creating a Fragment.
onCreate(Bundle) is for retrieving the Bundle from a previous instance.
In detail:
I've been scouring the web for this and asked a seasoned Android developer, so here's a decent explanation:
The Bundle passed as a parameter in the onCreate function is for if there was a previous instance of the Fragment, which is updated when the onSaveInstanceState function is called. (You can read up more on this in the official docs here: https://developer.android.com/training/basics/activity-lifecycle/recreating.html)
The Fragment.getArguments() function however, is used when initially creating your fragment. You're going to navigate to a Fragment for the first time, and a previous instance of that Fragment won't exist. In this instance, you can set your local variables within your Fragment that was shared by using the setArguments() & getArguments() functions. (More on that here: https://developer.android.com/reference/android/app/Fragment.html)
Therefore: Robust code could look like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
mView = inflater.inflate(R.layout.fragment_name, container, false);
mContext = getActivity();
if(savedInstanceState != null){
Log.d("yourapp", "A SavedInstanceState exists, using past values");
mValue = savedInstanceState.getString("valueString");
}else{
Log.d("yourapp", "A SavedInstanceState doesn't exist");
Bundle bundle = getArguments();
mValue = bundle.getString("valueString");
}
}
which handles both cases in your onCreate state.
Hope this helps!
I would like to add to N15M0_jk's answer.
Sometimes there's no need to save fragment state (for static fragments) and you could use only getArguments()
for re-creation since the arguments set with setArguments()
are retained even after destroy.
See the reference on setArguments()
Bundle received in the onCreate contains the data it most recently supplied in if the activity is recreated and getArguments Bundle returns the bundle which was supplied as argument.
The arguments bundle used for fragment creation and once set cannot be set again. The bundle in onCreate/onCreateView/onActivityCreated/onViewStateRestored is the savedInstanceState. You may use this get persisted values you persist via onSaveInstanceState
overrides. Upon fragment creation the savedInstanceState bundle is usually null so you may want to use getArguments.
One other thing about the getArguments, you dont have to persist these values. They will get recreated for you by the fragment code. If you try to setArguments on a fragment that already has them you will run into a IllegalStateException
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With