@@ -34,6 +34,7 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
34
34
binding = ActivityMainBinding .inflate(layoutInflater)
35
35
setContentView(binding.getRoot())
36
36
37
+ setupNavigator()
37
38
setupTopAppBar()
38
39
setupBottomNavigationView()
39
40
setupPopBackStack()
@@ -58,16 +59,33 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
58
59
}
59
60
60
61
private fun switchBottomTab (id : Int ) {
61
- if (navigator.getCurrentDestination () == id) return
62
+ if (navigator.getCurrentDestinationId () == id) return
62
63
63
64
navigator.navigate(id)
64
65
}
65
66
67
+ private fun setupNavigator () {
68
+ navigator.addOnDestinationChangedListener { _, destination, _ ->
69
+ if (navigator.getPreviousDestinationId() == R .id.searchFragment) {
70
+ showBars(true )
71
+ return @addOnDestinationChangedListener
72
+ }
73
+
74
+ if (destination.id == R .id.searchFragment) {
75
+ showBars(false )
76
+ }
77
+ }
78
+ }
79
+
66
80
private fun setupPopBackStack () {
67
81
onBackPressedDispatcher.addCallback(
68
82
owner = this ,
69
83
onBackPressedCallback = object : OnBackPressedCallback (true ) {
70
84
override fun handleOnBackPressed () {
85
+ if (navigator.getCurrentDestinationId() == R .id.searchFragment) {
86
+ showBars(true )
87
+ }
88
+
71
89
navigator.popBackStack()
72
90
73
91
changeSelectedBottomItem()
@@ -78,7 +96,7 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
78
96
79
97
private fun changeSelectedBottomItem () {
80
98
with (binding.bottomNavigation) {
81
- when (navigator.getCurrentDestination ()) {
99
+ when (navigator.getCurrentDestinationId ()) {
82
100
R .id.homeFragment -> selectedItemId = R .id.home
83
101
R .id.categoryFragment -> selectedItemId = R .id.category
84
102
R .id.cartFragment -> selectedItemId = R .id.cart
@@ -101,8 +119,6 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
101
119
true
102
120
}
103
121
R .id.menu_top_app_search -> {
104
- supportActionBar?.hide()
105
- binding.bottomNavigation.isVisible = false
106
122
navigator.navigate(R .id.searchFragment)
107
123
true
108
124
}
@@ -111,4 +127,15 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
111
127
}
112
128
})
113
129
}
130
+
131
+ private fun showBars (isShow : Boolean ) {
132
+ if (isShow) {
133
+ supportActionBar?.show()
134
+ }
135
+ else {
136
+ supportActionBar?.hide()
137
+ }
138
+
139
+ binding.bottomNavigation.isVisible = isShow
140
+ }
114
141
}
0 commit comments