Start activity with either of two fragments

78 Views Asked by At

I have an activity and used a navgraph to navigate through three fragments. This goes from Fragment1 to Fragment2 to Fragment3 and it works.

However, from Fragment3 I start a new activity. In this activity, I want to display either of two fragments within its FragmentContainerView. Let's call one FragmentList and one FragmentEmpty.

I have created a navgraph for this:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav"
    app:startDestination="@id/empty_fragment">

    <fragment
        android:id="@+id/empty_fragment"
        android:name="com.project.EmptyFragment"
        android:label="EmptyFragment" />
    <fragment
        android:id="@+id/list_fragment"
        android:name="com.project.ListFragment"
        android:label="ListFragment" />
</navigation>

I get that the startDestination basically works like a "default" here when I really want to display either one of two as the real start destination. So in the case where I want list_fragment this would be unnecessarily set and then overwritten. But it's a required field, so I have to set it.

To start the activity displaying the list_fragment, in put this in my activities onCreate function:

supportFragmentManager.beginTransaction()
            .add(R.id.fragment_container, FragmentList::class.java, Bundle())
            .commit()

The result looks like this:

enter image description here

The gray part is the empty_fragment and the white part is the list_fragment. For some reason, the activity clumsily displays them both. Even though I only have one FragmentContainerView:

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav"
        tools:layout="@layout/fragment_empty" />

Another problem I suspect is, even if I manage to do it this way, the startDestination would still be on my backstack. I can manually remove it at the same time that I set the list_fragment, however, this seems like the wrong way to use it.

When I tried to research the problem, I mostly get to tutorials and Stack Overflow questions where someone wants to navigate between two fragments within an activity. This is not exactly what I want. I will never switch from empty_fragment to list_fragment or vice versa. I just want the activity to display either when it's started, depending on a condition.

I'm fairly new to working with fragments and navgraphs, so I might scramble some things here; please take that into consideration.

To summarize:

  • How can I display either one of two fragments in my activity when the activity is created?
  • Is this the correct way to achieve this?
  • What is the reason for the odd stacked display my activity built with the two fragments?
2

There are 2 best solutions below

0
On

One solution is to write the logic yourself to decide which fragment to show using FragmentManager. This means that you shouldn't use the Navigation API at all.

0
On

Although it is not recommended, it will give you desired results

What you can do is create a 3rd Blank Fragment and set it as startDestination and on this fragment on create method you can apply your logic to navigate either of two fragment