'Android Navigate to Another Fragment from a Fragment using BottomNavigationView

I am using a BottomNavigationView with a FragmentContainerView for my fragment navigation. I added those two on my activity_home.xml, and with the BottomNavigationView, I can navigate through fragments pages. But, I have a button in my beranda fragment (One of my fragments in HomeActivity), and I want, when I click that button, I can navigate it to another fragment (hadiah fragment). What should I do? I want to navigate to another fragment from the current fragment. I've tried so many solutions on the internet, but none of them seems to work.

Here is my activity_home.xml

<?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=".HomeActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/main_bottom_menu"
        />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/main_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>

Here is how I set up my navigation bar on HomeActivity.java

bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fragmentContainerView);
NavController navCo = navHostFragment.getNavController();
NavigationUI.setupWithNavController(bottomNavigationView, navCo);

Here is the OnClickListener where I want it to navigate it to another fragment in my BerandaFragment.java

tukar_pop_up_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavHostFragment navHostFragment = (NavHostFragment) getActivity().getSupportFragmentManager()
                        .findFragmentById(R.id.fragmentContainerView);
                NavController navController = navHostFragment.getNavController();
                navController.navigate(R.id.hadiahFragment);
//                popupWindow.dismiss();
//                setupHelpPopUp(v);
            }
        });

With this current code, I successfully navigated to the destination fragment from the current fragment. But, the BottomNavigationView stopped working.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source