Android – What is the difference between getArguments and Bundle received in the onCreate-ThrowExceptions

Exception or error:

It may sound like a stupid question, but what is the difference between getArguments Bundle and Bundle received in the onCreate?

How to solve:


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:

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:

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");
        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

Leave a Reply

Your email address will not be published. Required fields are marked *