How do you pass data between Fragments with Jetpack Navigation?

WHAT IT TESTS: Type-safe argument flow in Navigation. ANSWER OUTLINE: Define args in nav graph XML, navigate with generated Directions, read with navArgs() in target Fragment. RED FLAG: Direct Fragment constructors, manual Bundles, or Activity Intent extras.
WHAT IT TESTS: Whether you understand type-safe inter-Fragment communication via Navigation and Safe Args. ANSWER OUTLINE: First, declare the argument in nav graph XML with name and type. Second, apply the Safe Args plugin so the build generates a Directions class. Third, call navigate() from the origin using that generated action with the value. Fourth, retrieve it in the destination Fragment with the navArgs() delegate. RED FLAG: Answering with new MyFragment(userId), setTargetFragment(), or Activity Intent extras.
Read the original → developer.android.com
- #android
- #jetpack navigation
- #fragments
- #safe args
- #interview
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.