Activity Layout: Fragment Class: VS Android:Name Attributes

The android:name attribute in the fragment

The ArticleListFragment and ArticleReaderFragment are names of classes that contain java code for these fragments.

As it was mentioned before you can have your fragment inside containing activity, but it is not a good practice to do so.

For a good example try to create "Blank Activity with Fragment" using Android Studio wizard. It will create an activity class and a fragment class along with 2 XML files for activity and fragment respectively.

Android Fragments: Usage of android:name

Quoting docs.

The android:name attribute in the <fragment> specifies the Fragment class to instantiate in the layout.

When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the onCreateView() method for each one, to retrieve each fragment's layout. The system inserts the View returned by the fragment directly in place of the element

This is when you declare fragment in xml.

You may also be interested to check this apart from the above.

Activity Layout: Fragment class: vs android:name attributes

What is the difference between the name attribute and context attribute in android XML?

In the case of the <fragment tag, the android:name attribute tells the LayoutInflater what Fragment class to instantiate when this layout is being inflated.

The tools:context tag is just to inform the layout editor where this layout is expected to be used. This is so that the editor can pull the theme from that Activity in order to display a more accurate preview (e.g. accent colors, text styles). Since a layout can be reused in multiple places, it's just a hint to the IDE on how to render it.

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

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

What does android:name attribute do?

Both do the same thing: identify the Java class that is the implementation of the fragment. AFAIK, android:name is the preferred attribute to use.

Android - Use of navigation with activities and fragments - Binary XML file Error

The documentation link you referenced uses the traditional fragment transaction by directly accessing supportFragmentManager; but it turns out from your post that you use Navigation Architecture components instead.

In navigation components, you are not supposed to use a particular fragment class in the android:name property, instead the navController takes care of that using NavHostFragment

To, fix this, please replace

android:name="com.my.application.LobbyFragment" 

With

android:name="androidx.navigation.fragment.NavHostFragment"

Also, you need to reference the name of the navGraph in that with app:navGraph attribute; assuming it's named as nav_graph:

app:navGraph="@navigation/nav_graph"

Is it safe to change the android:name attribute value in the application element?

Yes, this only affects the inside of the app when Android starts it up and is safe to change between flavors or versions of the app. The Android framework uses it to configure the creation of the application object, but it won't have any external effect or persist between executions of the app. An already installed app therefore shouldn't run into any problems being updated or similar.

It's not something that I have used extensively, so testing is always recommended. However, based on my experience as a software architect and working with the Android platform, I would consider this very low risk. I can't see any potential for bad interactions with Android or other apps outside of the app itself (since the application object can only be accessed inside the app).

Unknown attribute class warning with FragmentContainerView

It should be android:name="xxxxxx.fragments.SettingsFragment$PreferenceFragment" as per FragmentContainerView documentation.

<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="xxxxxx.fragments.SettingsFragment$PreferenceFragment"
android:tag="my_tag">
</androidx.fragment.app.FragmentContainerView>



Related Topics



Leave a reply



Submit