Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity Layout: Fragment class: vs android:name attributes

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

like image 237
Gianni Costanzi Avatar asked Apr 15 '12 14:04

Gianni Costanzi


People also ask

What is the difference between activity and fragment in Android?

Activity is an application component that gives a user interface where the user can interact. The fragment is only part of an activity, it basically contributes its UI to that activity. Fragment is dependent on activity. It can't exist independently.

What is Android name in fragment?

The android:name attribute specifies the class name of the Fragment to instantiate. When the activity's layout is inflated, the specified fragment is instantiated, onInflate() is called on the newly instantiated fragment, and a FragmentTransaction is created to add the fragment to the FragmentManager .

Why we should use fragment instead of activity?

A fragment has its own layout and its own behavior with its own lifecycle callbacks. You can add or remove fragments in an activity while the activity is running. You can combine multiple fragments in a single activity to build a multi-pane UI. A fragment can be used in multiple activities.

What are the advantages of using fragment over activity in Android?

The Fragment class in Android is used to build dynamic User Interfaces and should be used within the activity. The biggest advantage of using fragments is that it simplifies the task of creating UI for multiple screen sizes. An activity can contain any number of fragments.


1 Answers

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.

like image 130
far.be Avatar answered Sep 29 '22 19:09

far.be