android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.4k stars 8.29k forks source link

[NavigationAdvancedSample] Reusing fragments without referencing to one instance (iOS Tab Bar implementation in Android)? #878

Open olexth opened 4 years ago

olexth commented 4 years ago

Hello!

I'm using NavigationAdvancedSample structure as a base for my application. Currently, I have 5 tabs with corresponding nav stacks and in each of them I have one of app fragments, let's say UserProfileFragment, which is used in different places in all nav stacks in every of 5 tabs and which I want to reuse. Example of one of nav stack files:

    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>

What I see now is that if I populate UserProfileFragment with data in one place of the app, it changes UserProfileFragment appearances in all other places of tab nav stacks and behaves like a Singleton, so all its appearances have reference semantics. What I want is to reuse the same fragment for different purposes across the app but to have different instances for each use case, which will also allow me to use those instances in recursive navigation. Example navigation:

  1. user data screen(UserProfileFragment) 2. press on its "friends list", get to UserFriendsFragment. 3 press "see friend data" (UserProfileFragment again, but populated with friend data, not user), step 2 and 3 repeats continuously, etc.

Thanks in advance!

esQmo commented 4 years ago

call your fragment.newInstance method may be? passing data to be shown for each scenario throught bundle

olexth commented 4 years ago

@esQmo thanks for your reply! Can you add details on how to modify navigation file I posted above to create an instance of a Fragment instead of pointing the same one all the time?

olexth commented 3 years ago

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