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.