1
I’m learning about Android and would like to better understand how this part of the system works.
Instead of inflating a layout, it would not be simpler to do as when creating an Activity, for example, overwrite the onCreate method and configure an XML layout?
Example:
public class SampleActivity extends AppCompatActivity {
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
}
}
Here’s a piece of code from an example application that can be cloned from github: https://github.com/udacity/ud839_ViewPager_Example/tree/quiz
public class FridayFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_friday, container, false);
}
}
Why inflate is necessary? What is his advantage? Thanks for any clarifications.
I don’t think that answers the question. It would be a great edition of the question in order to give context. What she does is justify the question when she says: "Already in Activity, you do not need to create the View using a Layoutinflater because the setContentView() method already does this internally ...". So why doesn’t Fragment also have a setContentView method()?
– ramaral
I can’t think of a better answer, after all, we have to take into account that the API was set that way. In Fragment, you can set all parameters of the Inflater, so we need to call it in
onCreateView()
. In Activity, these parameters are predefined, and therefore the methodsetContentView()
simplifies this call. Therefore, I think that everything is just a means of facilitating the programmer.– regmoraes
You helped me to better understand how this process works. And I really thank you for your help. But there is still one question left. Why not just use setContentView() in Fragment? Maybe it really is how things were implemented. But even so, the Android development team always tries to streamline and simplify things for developers. Why wouldn’t this simplification exist? Because content can be variable like a viewPager using an Adapter?
– Nakamoto
At first it’s a bit complicated to understand, but the Fragments tbm work like Beans in memory, its after-open instance can be reused, different from Activity, which is discarded and tbm can be configured to climb more than one instance of your memory, and another thing that is very efficient is the listeners that can be applied to Fragments, an Activity can not receive a Systener because for each Activity the system creates a context, ie the system that manages its openness, Regarding the views, I believe the fragment uses Fragmentmanager to chain all the views
– Weslley Barbosa
When Fragmentmanage chains all views, when that Fragment is discarded all views linked to it should be submitted to Garbagecollector, and for that all objects should be disconnected from the main context which in most cases is Activity
– Weslley Barbosa