Android multi-fragment navigation

43 Views Asked by At

I have two different layout for portrait:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/container"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

and landscape orientation:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/container_channel"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/container_messages"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/container_messages"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/container_channel"
        app:layout_constraintTop_toTopOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

I have androidx.navigation:navigation-fragment-ktx:2.4.1 and my app perfectly change fragment in portrait orientation.

I would like to click on the button in the first fragment and change the state in the second

I got something like this:

if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
  supportFragmentManager.beginTransaction()
    .replace(R.id.container_channel, ChannelsFragment())
    .replace(R.id.container_messages, MessagesFragment())
    .commit()
} else {
  val navHostFragment =
    supportFragmentManager.findFragmentById(R.id.container) as NavHostFragment

  navController = navHostFragment.navController
}

Is it possible to make different navigation using this library for different orientations?

Is it possible to manage navigation from first fragment in second one using navigation lib?

0

There are 0 best solutions below