Age Calculator App – Navigation

Really I need some serious navigation to be added to the Weather app. But there is already so much going on there, that it will be easier to learn about it on the Age Calculator. It is just a single screen, but it could be made nicer using fragments, and this will bring the navigation into play.

You need to put the resources into the app gradle:

// Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0"
implementation "android.arch.navigation:navigation-ui-ktx:1.0.0"

Make a new resource file of type navigation. This will automatically make a new folder called ‘navigation’ and put your new xml file in there.

Screenshot showing the new navigation resource files

Now go and make some Fragment classes. At least two are required.

See the main image for this page. There is a little rectangle icon with a green + in the top bar. Click this to add a fragment class to your navigation. Then drag from one class to the next. An arrow will show the relationship.

By adding the navigation classes to the gradle, you have access to the Navigation object, automagically. You can use its functionality to call the created actions by id. Example:

binding.goButton.setOnClickListener(
Navigation.createNavigateOnClickListener(R.id.action_titleFragment_to_calcFragment)
)

This will make a nice transition, and when you click the back button, it will automatically return to the expected location.

Leave a Reply

Your email address will not be published. Required fields are marked *