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

Community
  • 1
  • 1
Gianni Costanzi
  • 6,054
  • 11
  • 48
  • 74

3 Answers3

46

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.

far.be
  • 689
  • 6
  • 8
  • 1
    I just checked out, the basic template provided by Android Studio 2.0 uses `name:xxx` attribute by default. – yask Feb 23 '16 at 08:59
  • "The android:name attribute in the specifies the Fragment class to instantiate in the layout." This is the definition I saw in developer website. Page: [link](https://developer.android.com/guide/components/fragments.html). – Shibin Francis Oct 06 '17 at 07:23
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>.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 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. – spaaarky21 Jun 06 '13 at 17:11
  • @spaaarky21: Yeah, I've switched over to `android:name` everywhere. – CommonsWare Jun 06 '13 at 17:23
  • 1
    additional credence for usage of name vs class: IntelliJ IDEA uses/autofills the name attribute in its latest layout editor (v13) – rmirabelle Jun 26 '13 at 13:42
  • aapt r20 had a bug which only affected `class` and @xavier-ducrohet recommended `android:name`. https://plus.google.com/+XavierDucrohet/posts/7uRN3ba8zxt – James Wald May 05 '14 at 18:39
  • Do we need a fully qualified class name? Or can we just `.ArticleReaderFragment`? – Kenny Worden Sep 03 '14 at 23:29
  • @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. – CommonsWare Sep 03 '14 at 23:31
  • @CommonsWare Eclipse keeps suggesting a fully qualified name... I'll just go with what works I guess. But thanks! – Kenny Worden Sep 03 '14 at 23:35
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

AndroUser
  • 572
  • 1
  • 5
  • 10
  • 1
    According to [this page](http://developer.android.com/reference/android/app/Fragment.html), they use the `android:tag` attribute for that, which makes sense: `android:tag can be used in to provide a specific tag name for the fragment` – user1978019 Apr 30 '14 at 14:29