Sourabh Khandelwal
Sourabh Khandelwal

Reputation: 43

Why is the setContentView(View) method not provided to be used with Fragment?

I am a beginner in Android Development and recently learned about Activity and Fragment. What I learned is, "To set the View for an Activity we simply call the setContentView(View) method inside the onCreate(Bundle) method. While to set a View for a Fragment we need to inflate a view and then return it from the onCreateView(LayoutInflater, ViewGroup, Bundle) method."

What I want to ask is why has Android decided to provide different implementations to achieve similar tasks? According to me since the onCreateView(LayoutInflater, ViewGroup, Bundle) method of a Fragment is called after it's onAttach method there should not be any problem in directly setting the View to this Fragment(since it's already attached to it's hosting Activity) rather than returning an Inflated View.

Sorry for a stupid question but I have already read the Android API Guide, Fragment and Activity topics from the book "Android Programming - The Big Nerd Ranch Guide.", read other StackOverflow answers about setContentView vs inflater.inflate() but nowhere found any text explaining this.

PS: I know what setContentView does and that LayoutInflater is used for other scenarios such as inflating a View for ListView but these still do not answer my question.

Upvotes: 1

Views: 323

Answers (2)

Bhuvanesh BS
Bhuvanesh BS

Reputation: 13617

Activity is a first visible item to the user.

An activity is a single screen in Android. you can't show multiple activities at the same time. The activity is only having the fundamental things like Layout inflator which is used to inflate the XML layout and Tools to handle resources, strings and mostly everything you use.

Fragments are part of an Activity.

Without activity, you can't show any fragments. You can show multiple fragments inside an activity. That means Activity is a container of the fragment. The inflator you got from the onCreateView() method is passed from the activity. Fragments are much customizable.

See the onCreateView() method below.

@Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        return null;
    }

1) There you will get layout inflater passed by the activity used to inflate the layout XML file.

2) ViewGroup, this is the parent view that the fragment's UI should be attached to.

3) Bundle, Here you can handle the orientation change event. It holds the data.

Why fragment is not using setConentView to set view.

As said above, fragments are customizable you can decide the view inflated is going to attach to the root view or you are going to decide where it is going to attach.

You can know more about attach to root here.https://stackoverflow.com/a/44753734/5558150

Upvotes: 0

Bob
Bob

Reputation: 13865

Fragment and Activity are not the same, similarly adding a View to a ViewGroup is not similar to setting a view to an Activity. Activity is one of the app components that the system communicates. But Fragment is kind of a View which gets added to a ViewGroup in an Activity.

Unlike Activity, the Fragment is managed by the FragmentManager and it decides when to create the Fragment View, destroy the view, add it to the View hierarchy and the like.

And that's why you pass false in the LayoutInflater#inflate() method in the onCreateView which means we are not allowed to add the Fragment View to the parent container ViewGroup. The FragmentManager will take care of adding the Fragment View at the proper time and so we just return an inflated View for that.

Upvotes: 1

Related Questions