Search code examples
androidandroid-layoutandroid-fragmentsandroid-xmlandroid-listfragment

Adding Fragment to Activity in XML: What is the class attribute and why doesn't it have a namespace


In the following snippet,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent" />

</LinearLayout>

what is the class attribute, if it is the our Fragment class, then why is FragmentLayout$ appended to its name, and why is it without android or any namespace?

I can totally see that it is the name of the Fragment class, because TitlesFragment is the Fragment class in the example I took this code from, and com.example.android.apis.app tells me that we have to give this attribute a fully qualified name of the class, but what is FragmentLayout$ part appended to the name of the class, that is TitlesFragment?

I have taken this code from the example in this developer guide, but there is no explanation of the class attribute. I could not find anything relevant here in the reference either. Pardon me if it is something too fundamental.


Solution

  • It does refer to the Fragment class. FragmentLayout$TitlesFragment means that TitlesFragment is a public, static nested class in the FragmentLayout class. If you inspect that class, you'll see that it's a ListFragment subclass.

    public static class TitlesFragment extends ListFragment {
    

    The only reference to that notation I've ever seen in the developer pages is on the Custom Components page. (It's under sub-heading 4. Use the Custom Component.)