Mohamed Salad
Mohamed Salad

Reputation: 179

Android Development, how does this line of code become produced?

i'm reading this android dev book and im stuck understanding how this line of code is error free (please keep in mind i've gotten rid of some code because for more focus on this part.

public View onCreateView(LayoutInflater layoutToInflate, ViewGroup parent, Bundle saveState)
    {
        View v = layoutToInflate.inflate(R.layout.activity_main_fragment,parent,false);

        return v;
    }

from what i believe, i need a method that returns a view because the Class extends from a Fragment class, not an activity so i have to explicitly find the view, the parameters are straight forward what i dont understand is how we create a view and set it equal to layoutToInflate...false;

Upvotes: 0

Views: 37

Answers (2)

4127157
4127157

Reputation: 1458

layoutToInflate is a variable of LayoutInflater and R.layout.activity_main_fragment is the name of the layout file to be inflated.

Upvotes: 1

einschnaehkeee
einschnaehkeee

Reputation: 1888

I think you have a misunderstanding what the concepts of Fragments are. They reside inside an Activity. If a Fragment has a UI, it needs the parent Activity to also have a UI. That also means Fragments have a ViewParentbelonging to an Activity. This parent is given to the Fragment by the ViewGroup parent argument. So when creating a Fragment with a UI, you need to inflate the layout belonging to your Fragment and pass it to the Activity, which adds it to the ViewGroup parent. So that's why you get a LayoutInflater to inflate your Fragment's view:

View v = layoutToInflate.inflate(R.layout.activity_main_fragment,parent,false);

Afterwards you return it to give it to the parent Activity.

Upvotes: 1

Related Questions