Opening a fragment which replaces nav_host with being able to go back

35 Views Asked by At

I am trying to show a new fragment where the nav_host_fragment is but I want the old one to show whenever I click on backButton or on an X in my app (I added that already). In the state that it is in right now it won't let me get out of the PlaylistFragment.

I also tried around with FragmentTransaction.add(...) but I couldn't figure how I get this working

My Code:

MainActivity.java:

 Fragment fragment = new PlayListFragment(position);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment oldFragment = fragmentManager.findFragmentByTag(fragment.getClass().getName());

        //if oldFragment already exits in fragmentManager use it
        if (oldFragment != null) {
            //fragment = oldFragment;
        }

        fragmentTransaction.replace(R.id.nav_host_fragment, fragment, fragment.getClass().getName());

        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

        fragmentTransaction.addToBackStack("playlist");

        fragmentTransaction.commit();

PlaylistFragment.java:

        getActivity().getSupportFragmentManager().popBackStack("playlist", 0);

EDIT: When I tried putting null instead of "playlist" in the backstack: Yup. Then I see both fragments overflowing each other: Normal state: https://prnt.sc/12yitg5

When clicking on the "downloaded songs": https://prnt.sc/12yiu79

When clicking back in the fragment: https://prnt.sc/12yiuy6

EDIT: When I click on the X again it shows me the home fragment while bottom nav highlights the Library tab.

0

There are 0 best solutions below