使用
- layout 提供
androidx.fragment.app.FragmentContainerView
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/导航图"
com.google.android.material.bottomnavigation.BottomNavigationView
- 导航图
<navigation
app:startDestination="@id/fragmentone">
<fragment
android:id="@+id/fragmentone"
android:name="com.xx.fragmentone"
android:label="fragmentxx"
tools:layout="@layout/fragment_one">
<action
android:id="@+id/action_pagetwo"
app:destination="@id/fragmenttwo" />
<action...
</fragment>
- Activiity中初始化
NavHostFragment navHostFragment = (NavHostFragment)getSupportFragmentManager().findFragmentById(R.id.FragmentContainerView的id);
NavController controller = navHostFragment.getNavController();
NavigationUI.setupWithNavController(底部导航栏的view,controller);
- 导航跳转
Navigation.findNavController(view).navigate(R.id.跳转的page);
原理
基本原理
NavHostFragFragment作为主导航
持有NavControll对象
---NavDestination存储回退栈
交给Navgator 做---->navigate
activitystart或者fragmentmanager的方式做跳转