Navigation drawer - cannot return home after navigation

Viewed 50

I created a new project with Navigation Drawer Acitvity template. I added a new button to my home fragment.

<Button
        android:id="@+id/btnNavigate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_home"
        android:text="Navigate"/>

Added a new navigation action to my home fragment:

<fragment
        android:id="@+id/nav_home"
        android:name="com.gamf.navigationtest.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_nav_home_to_nav_gallery"
            app:destination="@id/nav_gallery" />
    </fragment>

Extended my HomeFragment.java class with the button onclicklistener:

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final TextView textView = binding.textHome;

        homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);


        final Button btnNavigate = binding.btnNavigate;
        btnNavigate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Navigation.findNavController(view).navigate(R.id.action_nav_home_to_nav_gallery);
            }
        });

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}

With the click of a button I can now navigate to my Gallery fragment, but if I open the navigation drawer and click on the Home button I cannot navigate back to Home.

What approach should I take to make such navigation posssible, but still be able to navigate back home?

Project available at: https://github.com/totokpatrik/NavigationTest

Thanks

0 Answers
Related