Search code examples
javaandroidandroid-lifecyclefragment-oncreateviewactivity-oncreateview

Where do lifecycle methods, and other methods, get their parameter values?


In the following Android lifecycle method:


  @Override
  public View onCreateView(
      LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    ...
    return view;
  }

onCreateView takes in a ‍‍‍‍‍‍‍‍‍‍LayoutInflater, @Nullable Viewgroup, and a @Nullable Bundle.
The issue I have is in the sample program I am referring to: sceneform-android-sdk

There doesn't seem to be an instance where we call the method onCreateView and pass in a LayoutInflatter, Viewgroup, and Bundle. And yet, we are able to use the passed in parameters.
Yes we call super.onCreateView(...), but again, where did the values passed as arguments to the super, that were the parameters of our local version of onCreateView, come from?

My main question, reiterated, would be what is it that calls the onCreateView method and passes in the parameters?
My second question is, if I were to create a method in the same activity onCreateView is in:

public void foo(LayoutInflater myInflater) // Or it takes in a Bundle, Viewgroup, etc.

Would my foo method also get the same parameter values as the onCreateView method?
Lastly, my third question would be in a similar vein. A view is returned, but like before, it doesn't look like we handle the return. So, what handles the return value??
If possible, I would humbly request further reading regarding how Android core code works so as to implement my own methods that properly make use of parameters from Android and how to properly return values to Android as well.


Solution

  • The documentation for Fragment.onCreateView() indicates the following:

    This will be called between onCreate(Bundle) and onActivityCreated(Bundle).

    The Android framwork makes all lifecycle calls itself.The Android framework will call onCreate() on the fragmentto initialize the fragment. At some later time, the Android framework will callonCreateView()` on the fragment. The following parameters are passed:

    • LayoutInflater: The LayoutInflater object that can be used to inflate any views in the Fragment. The framework gets the LayoutInflater from the Activity's Context.

    • ViewGroup: If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view. This parameter is the container View that holds the Fragment.

    Bundle: If non-null, this fragment is being re-constructed from a previous saved state as given here. In this case, the Bundle contains the most recently saved state of the Fragment (ie: the data from the most recent call made to the Fragments onSaveInstanceState() method).

    Regarding your other questions:

    • If you define a method foo(), the Android framework would never call it. The Android framework has a specific set of defined methods that it calls on certain components (Activity, Service, BroadcastReceiver, Fragment, etc.) at specific times.

    • The View returned from onCreateView() will be used by the Android framework. The documentation clearly indicates how the returned View object is used:

    If you return a View from here, you will later be called in onDestroyView() when the view is being released.