I have a dialog that should switch between multiple layouts based on button clicks in a menu. These different layouts each has different logic accompanied to their UI (kind of like switching between multiple forms) so what I tried to do is having a FragmentContainer with its own dedicated NavGraph inside the dialog, hide it until the user chooses from the menu and inflate it to fill the dialog on button click.
The problem: The first time everything works as intended - I set the FragmentContainerView to always be visible and I can see the "home" screen, and when I press a button the fragment changes. When I close and reopen the dialog the FragmentContainer will display nothing (no views are visible despite the fragment onCreateView being called).
Custom dialog XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:background="#00FFFFFF"
android:orientation="vertical">
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layoutDescription="@xml/dialog_scene">
//... Some views and buttons
<androidx.fragment.app.FragmentContainerView
android:id="@+id/dialog_nav_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="@+id/frameLayout"
app:layout_constraintEnd_toEndOf="@+id/frameLayout"
app:layout_constraintStart_toStartOf="@+id/frameLayout"
app:layout_constraintTop_toTopOf="@+id/frameLayout"
app:layout_constraintVertical_bias="1.0"
app:navGraph="@navigation/dialog_nav_graph" />
</androidx.constraintlayout.motion.widget.MotionLayout>
</RelativeLayout>
Nav graph:
<?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"
android:id="@+id/filter_dialog_nav_graph"
app:startDestination="@id/realEstateFilter">
<fragment
android:id="@+id/dialog_home"
android:name="com.app.Fragments.dialog_home"
android:label="dialogHome" >
<action
android:id="@+id/action_dialogHome_to_otherScreen"
app:destination="@id/otherScreen" />
</fragment>
<fragment
android:id="@+id/otherScreen"
android:name="com.app.Fragments.other_screen"
android:label="otherScreen" />
</navigation>
Where I handle the navigation inside the dialog:
private void initializeViews(View view){
mNavHost = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.dialog_nav_fragment);
mNavController = mNavHost.getNavController();
mNavController.popBackStack(R.id.dialogHome, false); // I want the navigation default destination to be a certain form when the dialog is first opened
MotionLayout motionLayout = view.findViewById(R.id.motion_layout);
motionLayout.setTransitionListener(new MotionLayout.TransitionListener() {
@Override
public void onTransitionStarted(MotionLayout motionLayout, int startId, int endId) {
switch (endId){
case R.id.start: {
state = UiState.MENU;
break;
}
case R.id.transition: {
state = UiState.FILTER;
mNavController.navigate(R.id.action_dialogHome_to_otherScreen;
break;
}
case R.id.transition2:{
state = UiState.FILTER;
mNavController.popBackStack(R.id.dialogHome, false);
}
}
}
});
}
Any idea on what might be the problem? In general is this a good approach for switching multiple screens or navigation inside a dialog doesn't work well together? Any general suggestions?