En el proyecto que estoy desarrollando me he encontrado un problema, entre 2 "Fragments" estoy creando un bucle en el que uno puede redirigir al otro, lo cual es correcto, pero al pretender volver hacía atrás se está deshaciendo la pila de navegación, por lo que es una mala práctica ya que se redirigen el uno al otro hasta llegar al origen de uno de ellos y estoy tratando de devolver ambas a su origen directamente en caso de que el usuario retroceda.
Los bucles en cuestión son estos:
Subjects -> SubjectFile -> TeacherFile -> SubjectFile -> TeacherFile -> SubjectFile -> TeacherFile
Teachers -> TeacherFile -> SubjectFile -> TeacherFile -> SubjectFile -> TeacherFile -> SubjectFile
El "activity_launcher.xml" está diseñado así:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarLayout">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/navigationHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/navigation_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
En el Activity tengo definido el "NavHostFragment" de la siguiente manera:
private NavHostFragment navHostFragment;
Y así lo recupero del "xml":
navHostFragment =
(NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.navigationHostFragment);
Y el método "onBackPressed()" está implementado así:
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
if (navHostFragment.getParentFragment() instanceof SubjectFile) {
Fragment subjects = new Subjects();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.subjects, subjects)
.addToBackStack(null)
.commit();
} else if (navHostFragment.getParentFragment() instanceof TeacherFile) {
Fragment teachers = new Teachers();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.teachers, teachers)
.addToBackStack(null)
.commit();
} else {
super.onBackPressed();
}
}
}
Mi pregunta es sencilla, no consigo recuperar el "Fragment" que se está mostrando a la hora de pulsar el botón "Back" que todo dispositivo "Android" tiene. ¿Cuál sería la forma correcta de identificarlo?