Activity Layout: Fragment class: vs android:name attributes
Asked Answered
S

3

68

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).

Selfacting answered 15/4, 2012 at 14:40 Comment(0)
E
45

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.

Electroshock answered 19/12, 2012 at 10:43 Comment(2)
I just checked out, the basic template provided by Android Studio 2.0 uses name:xxx attribute by default.Gizmo
"The android:name attribute in the <fragment> specifies the Fragment class to instantiate in the layout." This is the definition I saw in developer website. Page: link.Upshot
D
26

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>.

Dzoba answered 15/4, 2012 at 15:40 Comment(7)
I really wish there was documentation. Since class doesn't have a namespace, I'm curious whether that attribute wasn't just used while fragments were being developed but were left in because they can't break compatibility now. For consistency's sake, android:name seems ever so slightly preferable and as user1550716 pointed out, they appear to be interchangeable.Wolfgang
@spaaarky21: Yeah, I've switched over to android:name everywhere.Dzoba
additional credence for usage of name vs class: IntelliJ IDEA uses/autofills the name attribute in its latest layout editor (v13)Belligerency
aapt r20 had a bug which only affected class and @xavier-ducrohet recommended android:name. plus.google.com/+XavierDucrohet/posts/7uRN3ba8zxtPardner
Do we need a fully qualified class name? Or can we just .ArticleReaderFragment?Bonham
@Kenny: AFAIK, it has to be a fully-qualified class name, but I haven't tried a bare class name recently. I would be a bit surprised if the leading-dot notation worked.Dzoba
@Dzoba Eclipse keeps suggesting a fully qualified name... I'll just go with what works I guess. But thanks!Bonham
N
0

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

Noontime answered 4/7, 2013 at 9:20 Comment(1)
According to this page, they use the android:tag attribute for that, which makes sense: android:tag can be used in <fragment> to provide a specific tag name for the fragmentFortunate

© 2022 - 2024 — McMap. All rights reserved.