1

The current navigation construct like the following:

Activity --> Fragment1 --> Fragment2

I want to change the start destination dynamic according to the flag like the following:

Activity --> Fragment1 --> Fragment2  
      |   
      -----> Fragment2

And the Fragment2 will not back to the Fragment1 when dynamic change the start destination to Fragment2,

The navigation is like the following

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_frag">

    <fragment
        android:id="@+id/fragment1"
        android:name="bbin.mobile.ui.fragment1
        android:label="fragment1"
        tools:layout="@layout/layout_fragment1">
        <action
            android:id="@+id/action_fragment1_to_fragment2"
            app:enterAnim="@anim/anim_right_in"
            app:exitAnim="@anim/anim_left_out"
            app:popUpTo="@anim/anim_right_out"
            app:destination="@id/fragment2" />
    </fragment>

    <fragment
        android:id="@+id/fragment2"
        android:name="bbin.mobile.fragment2
        android:label="fragment2"
        tools:layout="@layout/layout_fragment2">
        <action
            android:id="@+id/action_fragment2_to_fragment1"
            app:popUpTo="@+id/fragment1"
            app:popUpToInclusive="true"
            app:destination="@id/fragment1" />
    </fragment>
</navigation>

And the layout of activity is like the following:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/frag"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/nav_frag"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

I have try the following link

Is it possible to set startDestination conditionally using Android Navigation Architecture Component(Android Jetpack)?

But it will back to the fragment1 when I change the start destination to the fragment2.

How to change the start destination dynamic in Android , and will not back to the fragment1 when set start destination to the fragment2 ?

Did I missing something ? Thanks in advance.

Wun
  • 6,211
  • 11
  • 56
  • 101

0 Answers0