Now in order to make your app dev base stronger we have come up with
ANDROID STUDY JAM #4
What's In?
Introduction to Fragments
Concept of Activity and Fragment lifecycle
Introduction to Navigation component
5. Fragments
A Fragment represents a reusable portion of your
app's User Interface.
It’s a piece of an activity which enable more
modular activity design.
In simple words,it can be referred to as a sub-
activity.
6.
7. WHY DO WE USE FRAGMENTS?
● To make apps lightweight.
● To construct a UI component that will be used by multiple
activities.
● To make efficient use of an activity to display different layouts.
● Flexibility to control different parts of the activity separately
because Fragments have their own layout,events and a
complete life cycle.
14. Boring Theory (But important)
The Navigation component consists of three key parts that are described below:
● Navigation graph: An XML resource that contains all navigation-related information in one
centralized location. This includes all of the individual content areas within your app, called
destinations, as well as the possible paths that a user can take through your app.
● NavHost: An empty container that displays destinations from your navigation graph. The
Navigation component contains a default NavHost implementation, NavHostFragment, that
displays fragment destinations.
● NavController: An object that manages app navigation within a NavHost. The NavController
orchestrates the swapping of destination content in the NavHost as users move throughout
your app.
16. Navigation Graph
1. Navigation occurs between your app's destinations—that is, anywhere in your app
to which users can navigate. These destinations are connected via actions.
2. A navigation graph is a resource file that contains all of your destinations and
actions. The graph represents all of your app's navigation paths.
➔ Destinations are the different content areas or fragments in your app.
➔ Actions are logical connections between your destinations that represent paths
that users can take. So it is used to navigate from one fragment to another.
19. NavController
1. Navigating to a destination is done using a NavController, an object that manages app navigation within a
NavHost. Each NavHost has its own corresponding NavController. NavController provides a few different
ways to navigate to a destination, which are further described in the sections below.
2. To retrieve the NavController for a fragment, activity, or view, use one of the following methods:
Use Safe Args to navigate with type-safety
1. The recommended way to navigate between destinations is to use the Safe Args Gradle plugin. This plugin
generates simple object and builder classes that enable type-safe navigation between destinations. Safe
Args is recommended both for navigating as well as passing data between destinations.
Kotlin:
● Fragment.findNavController()
● View.findNavController()
● Activity.findNavController(viewId: Int)
20. NavHost
A host is a single context or container for navigation via a NavController.
Navigation hosts must:
● Handle saving and restoring their controller's state
● Call Navigation.setViewNavController on their root view
● Route system Back button events to the NavController either by manually calling
NavController.popBackStack or by calling NavHostController.setOnBackPressedDispatcher
when constructing the NavController.