はじめに
佐藤佑哉です。
今回、画面遷移を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.xml
をmenu
ディレクトリに作成する- ボトムナビゲーションビューに必要なアイテムを宣言
-
item
のid
とnav_graph.xml
のfragment
のid
は同じ命名をする必要がある
-
- ボトムナビゲーションビューに必要なアイテムを宣言
<?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
でボトムナビゲーションビューの設定を行う