Search code examples
androidandroid-fragmentsnavigationreusability

How to reuse android fragment across navigation stacks (iOS Tab Bar implementation in Android)?


I'm building an Android app to replicate already existing iOS one and I'm using Bottom Navigation element as the core app structure which is similar to iOS Tab Bar pattern. So, I have one main Activity in the app which only handles bottom navigation. Currently, I have 5 tabs and I populate each tab with navigation with nested graphs precisely as in this official Android architecture example, which gives me an ability to have 5 independent navigation stacks, one for each tab. For screens representations, I'm using Android fragments, some of which I'm trying to reuse in different places of each navigation stack. Each navigation file looks typically like this:

<fragment
    android:id="@+id/gallery"
    android:name="test.flows.socialNetwork.gallery.GalleryFragment"
    android:label="@string/fotoghrafii"
    tools:layout="@layout/fragment_gallery">
    <action
        android:id="@+id/action_gallery_to_countries"
        app:destination="@id/countriesList" />
    <action
        android:id="@+id/action_gallery_to_profile"
        app:destination="@id/galleryToInterlocutorProfile" />
</fragment>
<fragment
    android:id="@+id/countriesList"
    android:name="test.flows.common.countries.CountriesListFragment"
    android:label="@string/vybor_strany"
    tools:layout="@layout/fragment_select_country"/>
<navigation android:id="@+id/galleryToInterlocutorProfile"
    app:startDestination="@id/galleryToInterlocutorProfile">
    <fragment
        android:id="@+id/galleryToInterlocutorProfile"
        android:name="test.flows.socialNetwork.userProfile.UserProfileFragment"
        android:label="galleryToInterlocutorProfile"
        tools:layout="@layout/fragment_interlocutor_profile" />
</navigation>
Problem I have is that all fragments in navigations behave like Singletons - if I populate one's UI with data, all other with the same class refers to it in other places/navigation stacks (refers to the same instance) and change appropriately. What I want is to reuse the fragment with the same UI but for different purposes - one will show app user data, other - user's friend, another one - another friend etc. So, basically I want different instances of the same fragment, not references to the same one across whole app. Code for them is pretty similar, so I'd use one **UserProfileFragment** class for all of them to avoid code duplication. Please, let me know if you need more info or claryfications as I'm not full time Android developer and may not correctly use conventions and terms.

Thanks in advance!


Solution

  • So, I found the "classic" way to implement the same pattern in code without the limitation of having one instance for every Fragment. For everyone interested in implementing fully functional iOS UITabBar in Android, a working version is here: https://github.com/Codeveyor/Android-Tab-Bar