Week_6_Fragment
Week_6_Fragment
Fragment
(EXAM)
CET3013: MOBILE APPLICATION DEVELOPMENT
Today's lecture 2 ui combine into 1 interface called fragment
• Building a Flexible UI
• Communicating with Other
Fragments
• Fragment navigation 2nd UI
• Fragments
• Mini-activities, each with its own set of views
• One or more fragments can be embedded in an Activity
• You can do this dynamically as a function of the device type (tablet or not)
or orientation
Fragment Idea
Fragments
• Mini-activities, each with its own set of views
• One or more fragments can be embedded in an Activity
• You can do this dynamically as a function of the device type (tablet or not)
or orientation
You might
detail decide to run
a tablet in
Master portrait mode
with the handset
model of only
one fragment
in an Activity
Fragment
class FragmentOne : Fragment() { Extend class into fragment (also have use binding)
private var _binding: FragmentTextBinding? = null
private val binding get() = _binding!!
The points of note in the above navigation host fragment element are the reference to the
NavHostFragment in the name property, the setting of defaultNavHost to true and the
assignment of the file containing the navigation graph to the navGraph property.
Navigation Graph
•The navigation graph holds all of the navigation-related
information that your app requires, and describes the
possible paths the user can take when navigating the app.
Navigation Controller
•The navigation controller controls which fragment is displayed
in the navigation host as the user navigates through the app.
Navigation Configuration
•You need to add a library dependency to the app’s version
of the build.gradle file.
Navigation Action
•Actions are used to connect destinations in the navigation
graph, and they define possible paths the user can take when
navigating through the app.
•Every action must have a unique ID. Android uses this ID to
determine which destination needs to be displayed as the
user navigates through the app.
Navigation Action
Navigation Graphs XML
Navigation Action: Passing
Arguments
•Data may be passed from one destination to another during
a navigation action by making use of arguments.
•An argument consists of a name, type and an optional
default value and may be added manually within the XML.
•A navigation action is triggered by calling the controller’s
navigate() method and passing through the resource id
Navigation Action: Passing
Arguments
Navigation Action: Passing
Arguments
Navigation Action: Safe Args
•The Navigation component that lets you pass arguments to
destinations in a type-safe way.
•We need to update the project and app build.gradle files.