5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SingleActivityでBottomNavigationViewとNavigationコンポーネントを用いて画面遷移をしたい

Last updated at Posted at 2023-03-15

はじめに

佐藤佑哉です。
今回、画面遷移をNavigationコンポーネントの使用を検討したところ、Navigationコンポーネントの公式ドキュメントでSingle Activityを推奨しているのと、メンターさんがSingleActivityを激推ししてきたので、忘れないようにと記事をまとめておきました!

5つのステップ

1.ナビゲーションコンポーネントの依存関係を追加

dependencies{
..
    //ナビゲーションコンポーネントの依存関係を追加
    implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")
    implementation("androidx.navigation:navigation-ui-ktx:2.5.3")
..
}

2.navigationグラフを作成する

  • app/res/navigationディレクトリを作成する
    • フォルダ名はnavigationにする必要がある
  • navigationディレクトリにnav_graph.xmlファイルを作成
    • BottomNavigationViewを押下した時の最初に遷移したいFragmentコンポーネントを宣言
<?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/main_nav_graph"
    app:startDestination="@id/home">
    <!-- startDestinationはホーム画面 -->
    <fragment
        android:id="@+id/home"
        android:name="com.example.myapplication.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/profile"
        android:name="com.example.myapplication.ProfileFragment"
        android:label="fragment_profile"
        tools:layout="@layout/fragment_profile" />
    <fragment
        android:id="@+id/list"
        android:name="com.example.myapplication.ListFragment"
        android:label="fragment_list"
        tools:layout="@layout/fragment_list" />
</navigation>

3.menuファイルを作成する

  • app/res/menuディレクトリを作成
  • bottom_navigation_view.xmlmenuディレクトリに作成する
    • ボトムナビゲーションビューに必要なアイテムを宣言
      • itemidnav_graph.xmlfragmentidは同じ命名をする必要がある
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <item
        android:id="@+id/home"
        android:icon="@drawable/baseline_home_24"
        android:title="@string/home"
        tools:layout="@layout/fragment_home" />
    <item
        android:id="@+id/list"
        android:icon="@drawable/baseline_view_list_24"
        android:title="@string/list"
        tools:layout="@layout/fragment_list" />
    <item
        android:id="@+id/profile"
        android:icon="@drawable/round_account_circle_24"
        android:title="@string/profile"
        tools:layout="@layout/fragment_profile" />
</menu>

4.activity_main.xmlに必要なコンポーネントを追加する

  • FragmentContainerView
  • BottomNavigationView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment_container"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bottom_navigation_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/main_nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation_view" />

</androidx.constraintlayout.widget.ConstraintLayout>

5.MainActivityでボトムナビゲーションビューの設定を行う

MainFragmentのonCreateメソッド内

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ..
        //ボトムナビゲーションビューの設定
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment_container)
        val navController = navHostFragment?.findNavController()
        binding.bottomNavigationView.setupWithNavController(navController!!)
..
}

まとめ

1. ナビゲーションコンポーネントの依存関係を追加
2. navigationグラフを作成する
3. menuファイルを作成して必要な要素を追加
4. activity_main.xmlに必要なコンポーネントを追加する
5. MainActivityでボトムナビゲーションビューの設定を行う

5
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
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?