This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
BottomNavFragment.kt
77 lines (65 loc) · 2.98 KB
/
BottomNavFragment.kt
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
70
71
72
73
74
75
76
77
package com.example.android.navigationsamples
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.NavigationUI
import com.example.android.navigationsamples.BackButtonBehaviour.POP_HOST_FRAGMENT
import com.google.android.material.bottomnavigation.BottomNavigationView
class BottomNavFragment : Fragment() {
private val bottomNavSelectedItemIdKey = "BOTTOM_NAV_SELECTED_ITEM_ID_KEY"
private var bottomNavSelectedItemId = R.id.home // Must be your starting destination
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_bottom_nav, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
savedInstanceState?.let {
bottomNavSelectedItemId =
savedInstanceState.getInt(bottomNavSelectedItemIdKey, bottomNavSelectedItemId)
}
setupBottomNavBar(view)
}
// Needed to maintain correct state over rotations
override fun onSaveInstanceState(outState: Bundle) {
outState.putInt(bottomNavSelectedItemIdKey, bottomNavSelectedItemId)
super.onSaveInstanceState(outState)
}
private fun setupBottomNavBar(view: View) {
val bottomNavView = view.findViewById<BottomNavigationView>(R.id.bottom_nav_view)
val toolbar = view.findViewById<Toolbar>(R.id.bottom_nav_toolbar)
// Your navGraphIds must have the same ids as your menuItem ids
val navGraphIds = listOf(R.navigation.home, R.navigation.list, R.navigation.form)
addToolbarListener(toolbar)
bottomNavView.selectedItemId = bottomNavSelectedItemId // Needed to maintain correct state on return
val controller = bottomNavView.setupWithNavController(
fragmentManager = childFragmentManager,
navGraphIds = navGraphIds,
backButtonBehaviour = POP_HOST_FRAGMENT,
containerId = R.id.bottom_nav_container,
firstItemId = R.id.home, // Must be the same as bottomNavSelectedItemId
intent = requireActivity().intent
)
controller.observe(viewLifecycleOwner, { navController ->
NavigationUI.setupWithNavController(toolbar, navController)
bottomNavSelectedItemId = navController.graph.id // Needed to maintain correct state on return
})
}
private fun addToolbarListener(toolbar: Toolbar) {
toolbar.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.settings -> {
findNavController().navigate(R.id.settings)
true
}
else -> false
}
}
}
}