javaandroidback-stack

bottom navigation backbutton issue


Hello in my project I made a bottom navigation with a navigation component.

Replacing fragment by selecting other tab works well.

But the problem is when I press back button.

I have three Fragment

  1. HomeFragment
  2. LikeFragment
  3. MypageFragment

HomeFragment is the start Fragment.

If I touch

HomeFragment -> LikeFragment -> MypageFragment

and then touch back button the fragment goes like this.

MypageFragment -> HomeFragment

But this action is not what I meant it.

I wanted to make like this.

MypageFragment -> LikeFragment -> HomeFragment

how can i solve this issue?

this is my code

botnav_graph.xml

<?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/botnav_graph"
    app:startDestination="@id/homeFragment">
    <fragment
        android:id="@+id/homeFragment"
        android:name="wook.co.coc.view.bottom_nav.HomeFragment"
        android:label="HomeFragment" />
    <fragment
        android:id="@+id/likeFragment"
        android:name="wook.co.coc.view.bottom_nav.LikeFragment"
        android:label="LikeFragment" />
    <fragment
        android:id="@+id/myPageFragment"
        android:name="wook.co.coc.view.bottom_nav.MyPageFragment"
        android:label="MyPageFragment" />
</navigation>

bottomnav_container.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <fragment
        android:id="@+id/botnav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/botnav_graph"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="#fff"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/bottom_nav"/>

</RelativeLayout>

MainPage.java

public class MainPage extends AppCompatActivity {

    BottomNavigationView btnv;

    NavController navc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("FragmentActivity","onCreated");
        setContentView(R.layout.bottomnav_container);

        btnv = findViewById(R.id.bottomNav);

        navc = Navigation.findNavController(this,R.id.botnav_host_fragment);
        NavigationUI.setupWithNavController(btnv,navc);

    }
}

And addtionally I would like to add backstack with out any duplication how can I do this?


Solution

  • When you use nav graph from google, it is normal behavior: MypageFragment -> HomeFragment HomeFragment is your destination fragment. Other fragments did not save in fragment stack. See this.

    You can write your own navigation, use FragmentManager