Activity Layout: Fragment class: vs android:name attributes

AndroidAndroid FragmentsAndroid XmlXml Attribute

Android Problem Overview


I've read the documentation about Fragments in the Android Developer Guide and I've seen that sometimes they specify the class to instantiate with the Fragment tag attribute android:name and sometime they use the class: attribute:

<fragment
    android:name="com.example.news.ArticleReaderFragment"
    android:id="@+id/viewer"
    android:layout_weight="2"
    android:layout_width="0dp"
    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" />

Are android:name and class: interchangeable? If I use the autocompletion function in Eclipse, they both show the same documentation tip (i.e. the attribute provides the class name to be instantiated). Maybe you must use the second one when the class to be instantiated has a name which is different from the java file name, like TitlesFragment which is in the FragmentLayout.java file? Or can I use the syntax package.fileDOTjava$Class also with the android:name attribute?

I'd like to have some documentation for XML tags and attributes as for Android Java Classes (I've asked about it in another question).

Android Solutions


Solution 1 - Android

As Activity.onCreateView source says:

String fname = attrs.getAttributeValue(null, "class");
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment);
if (fname == null) {
    fname = a.getString(com.android.internal.R.styleable.Fragment_name);
}

That seemingly means that program looks "class" attribute first. And on fail looks "name" attribute. So as far as it's true using "class" if more efficient.

Solution 2 - Android

> Are android:name and class: interchangeable?

Presumably, yes. I have only used class, and that seems to be what most of Google's examples use, but I do see where they use android:name in some samples. Unfortunately, there is no formal and complete documentation for <fragment>.

Solution 3 - Android

Sorry all experts are here, I may be wrong but as per my knowledge android:name attribute of fragment is used to find fragment, when we use getFragmentByTag() method of fragmentManager class. also android:class attribute is used to find fragment class which we generally include for static fragment.

Hope this will help.. thanks

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGianni CostanziView Question on Stackoverflow
Solution 1 - Androidfar.beView Answer on Stackoverflow
Solution 2 - AndroidCommonsWareView Answer on Stackoverflow
Solution 3 - AndroidAndroUserView Answer on Stackoverflow