AdvNative Week 3 - Navigation Part 2
AdvNative Week 3 - Navigation Part 2
(part 2)
WEEK 3
Advanced Native Mobile Programming
1604C062
MainFragment
BackStack
POPUP
Player then presses start button (navigate to game fragment)
Now backstack
contain two
fragments. What
happen if back
button is pressed?
Game Fragment
MainFragment
BackStack
POPUP
Player presses back button (soft) or hard or call navigate codes
MainFragment
BackStack
POPUP
And then player presses start button (navigate to game fragment)
Another instance of
GameFragment
created on backstack.
Game Fragment How to change this
behavior?
MainFragment
Game Fragment
MainFragment
BackStack
USING POPUPTO
popUpTo attributes can be applied to an action (arrow) to remove
backstack content until it reaches the destination fragment
example:
Fragment B
Fragment A
USING POPUPTO
Using popUpTo fragmentA, it will popup every item on backstack
until it reaches the destination (fragmentA)
Fragment C
Fragment B
Fragment A
LET’S TRY IT
Fragment C
Right click on res > new > Vector assets, and then create three icons:
= MainFragment
= HistoryFragment
= ProfileFragment
CREATE FRAGMENTS
Create two new fragments, name them
HistoryFragment and ProfileFragment
1. Open activity_main.xml
2. Drag and drop BottomNavigationView component
3. Change id to “bottomNav”
4. Set menu to “@menu/bottom_menu”
5. Set layout_width = match_constraint
6. Set layout_height = wrap_content
7. Constrained to very bottom of screen
ADD BOTTOM NAVIGATION
UPDATE NAVIGATION GRAPH
Next step, we should update navigation graph by adding new
destinations (fragment profile and history). Open game navigations
and add two destinations
!! IMPORTANT !!
To create a connection between navigation controller with
navigation bar you need to set same ID on both of Destinations
Object and Menu Item ID
!! IMPORTANT !!
It means that for MainFragment destinations you should rename
the id as “itemHome”
!! IMPORTANT !!
This dialog shows that the id already exists. Just press continue to
ignore this warning.
NO ACTION/ARROW
For this case, its unnecessary to draws connection/arrow between
those fragments. Bottom bar navigation allows user to jump in
various fragment without specific flow order.
ESTABLISH CONNECTION
Final step is to establish connection between nav controller and the
bottom bar. Remember: nav controller holds navigation host that
relies on navigation graph.
txtInputLayout
endIconMode = dropdown_menu
DIALOG FRAGMENT LAYOUT
ConstraintLayout parent
- Layout_width =
match_parent
- Layout_height =
wrap_content
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent" Wrap constraint
layout within drawer
tools:context=".MainActivity"> layout tags
....
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Enclosed drawer
layout tag
NAVIGATION VIEW
Switch back to design view, drag and drop a navigation view
component, and arrange it like following screenshot
Navigation View
● Id = navView
● layout_gravity =”start”
● layout_width = wrap_content
● layout_height = match_parent
● Menu = @menu/bottom_menu
IMPORTANT! Navigation
View must be placed on
the bottom of layout. Not
under Constraint layout
NAVIGATION VIEW
Notice that navigation view is using the same menu resource files as
bottom navigation bar.
It means that the drawer will render the same menu structure and
item as the navigation bar. Therefore, no need to update navigation
graph.
navController = (supportFragmentManager.findFragmentById(R.id.hostFragment)
as NavHostFragment).navController