Why inflate a Layout in Fragments instead of setting one already ready? Ex: setContentView(R.layout.activity_example)

Asked

Viewed 237 times

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.

1 answer

2

The LayoutInflater is the class responsible for creating a View from an XML file, therefore, you need it to instantiate and access a View.

In Fragments, like the onCreateView() requires a View return, you have to use the LayoutInflater to create and return such View.

In Activity, you don’t need to create the View using a LayoutInflater for the method setContentView() already does so internally, so it is only a practical means of creating and assigning a View to an Activity. If you look at the implementation of the method setContentView() from Activity, you will see that he also uses a LayoutInflater.

@Override
    public void setContentView(int resId) {
        ensureSubDecor();
        ViewGroup contentParent = (ViewGroup) mSubDecor.findViewById(android.R.id.content);
        contentParent.removeAllViews();

        // Cria a View baseada no id do layout passado e atribui a Activity
        LayoutInflater.from(mContext).inflate(resId, contentParent);
        mOriginalWindowCallback.onContentChanged();
    }
  • 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()?

  • 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 method setContentView() simplifies this call. Therefore, I think that everything is just a means of facilitating the programmer.

  • 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?

  • 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

  • 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.