How to make a navigation graph for bottom navigation ? and is there a way to modify the back stack behavior?
我是导航架构组件的新手,正在尝试制作
我的应用由三个用于底部导航的片段组成。我试图做的是,我已经将图中每个可能组合中的片段链接起来。
结果不是灾难性的,但我仍然不确定这是否是正确的方法。
此外,当我使用设备的后退按钮时,应用程序会在退出应用程序之前浏览我浏览过的每个片段。
我想进行导航,以便对于第一个后退按钮,它导航回第一个片段 - 起点 - 然后对于第二个片段存在应用程序。
这是导航图的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <?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" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/nav_graph" app:startDestination="@id/bottom_navigation_home"> <fragment android:id="@+id/bottom_navigation_home" android:name="com.app.albaladinn.view.ui.HomeFragment" android:label="fragment_home" tools:layout="@layout/fragment_home"> <action android:id="@+id/action_homeFragment_to_couponsFragment" app:destination="@id/bottom_navigation_coupons" /> <action android:id="@+id/action_homeFragment_to_settingsFragment" app:destination="@id/bottom_navigation_settings" /> <action android:id="@+id/action_homeFragment_to_categoriesFragment" app:destination="@id/bottom_navigation_categories" /> </fragment> <fragment android:id="@+id/bottom_navigation_coupons" android:name="com.app.albaladinn.view.ui.CouponsFragment" android:label="fragment_coupons" tools:layout="@layout/fragment_coupons"> <action android:id="@+id/action_couponsFragment_to_homeFragment" app:destination="@id/bottom_navigation_home" /> <action android:id="@+id/action_couponsFragment_to_settingsFragment" app:destination="@id/bottom_navigation_settings" /> <action android:id="@+id/action_couponsFragment_to_categoriesFragment" app:destination="@id/bottom_navigation_categories" /> </fragment> <fragment android:id="@+id/bottom_navigation_settings" android:name="com.app.albaladinn.view.ui.SettingsFragment" android:label="fragment_settings" tools:layout="@layout/fragment_settings"> <action android:id="@+id/action_settingsFragment_to_couponsFragment" app:destination="@id/bottom_navigation_coupons" /> <action android:id="@+id/action_settingsFragment_to_homeFragment" app:destination="@id/bottom_navigation_home" /> <action android:id="@+id/action_settingsFragment_to_categoriesFragment" app:destination="@id/bottom_navigation_categories" /> </fragment> <fragment android:id="@+id/bottom_navigation_categories" android:name="com.app.albaladinn.view.ui.CategoriesFragment" android:label="fragment_categories" tools:layout="@layout/fragment_categories"> <action android:id="@+id/action_categoriesFragment_to_couponsFragment" app:destination="@id/bottom_navigation_coupons" /> <action android:id="@+id/action_categoriesFragment_to_homeFragment" app:destination="@id/bottom_navigation_home" app:popUpToInclusive="false" /> <action android:id="@+id/action_categoriesFragment_to_settingsFragment" app:destination="@id/bottom_navigation_settings" /> </fragment> </navigation> |
这是主要活动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | public class MainNavigationActivity extends AppCompatActivity { BottomNavigationView mainBottomNavigation; MainBottomNavigationControl mainBottomNavigationControl; NavController navController; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_navigation); navController = Navigation.findNavController(this, R.id.frame_container); mainBottomNavigation = findViewById(R.id.bottom_navigation); NavigationUI.setupWithNavController(mainBottomNavigation, navController); mainBottomNavigationControl = new MainBottomNavigationControl(); mainBottomNavigation.setOnNavigationItemSelectedListener(mainBottomNavigationControl); } class MainBottomNavigationControl implements BottomNavigationView.OnNavigationItemSelectedListener { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.bottom_navigation_home: navController.navigate(R.id.bottom_navigation_home); return true; case R.id.bottom_navigation_categories: navController.navigate(R.id.bottom_navigation_categories); return true; case R.id.bottom_navigation_coupons: navController.navigate(R.id.bottom_navigation_coupons); return true; case R.id.bottom_navigation_settings: navController.navigate(R.id.bottom_navigation_settings); return true; default: return false; } } } } |