Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

What is the difference between getArguments Bundle and Bundle received in the onCreate?

like image 853
AndroidDev Avatar asked Nov 08 '14 19:11

AndroidDev


People also ask

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

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.

What is onCreate view in Android?

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 .


4 Answers

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!

like image 133
jarodsmk Avatar answered Oct 05 '22 09:10

jarodsmk


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()

like image 29
Erik Sillén Avatar answered Oct 05 '22 11:10

Erik Sillén


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.

like image 32
eminem Avatar answered Oct 05 '22 09:10

eminem


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

like image 24
petey Avatar answered Oct 05 '22 09:10

petey