LoginSignup
0
0

More than 1 year has passed since last update.

BottomNavigationViewの戻る処理について

Posted at

Android JetpackのNavigationコンポーネントを利用。
(確認したバージョンは、 2.4.2)

NavigationBarView.setupWithNavController を利用した場合の、タブ遷移後の戻るボタンの処理について確認した。

android:menuCategory="secondary" の有無によって、動作が違う。

Screenshot_20220611_165007.png

例: Home -> Dashboard -> Notifications -> Dashboard と操作したあとに、back keyを押したとき。

パターンA: menuCategory="secondary" を指定した場合

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/home"
        android:icon="@drawable/ic_home_black_24dp"
        android:menuCategory="secondary"
        android:title="@string/title_home" />

    <item
        android:id="@+id/dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:menuCategory="secondary"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:menuCategory="secondary"
        android:title="@string/title_notifications" />
</menu>

Dashboard から、
-> Notifications -> Dashboard -> Home
のように、順番に戻っていく。
(stackがすべて積まれている状態)

パターンB: 未指定

上記のそれぞれの item から、 android:menuCategory="secondary" を削除した場合。

Dashboard から、
-> Home
のように、起動直後の初期タブに戻るのみ。
(stackを破棄しながら遷移しているイメージ)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0