Day61 of #100DaysOfCode
Hii folks 🙌
Today I will be continuing the same pathway in which I will learn to update the Fragments label.
Unit 3: Navigation
Pathway 2: Introduction to the Navigation component
Source: https://developer.android.com/courses/android-basics-kotlin/course
- Now we will get the Arguments in
WordListFragment
by creating aletterId
property. We can mark this aslateinit
so that we don't have to make it nullable.
private lateinit var letterId: String
- Then override
onCreate()
(notonCreateView()
oronViewCreated()
!), add the following.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) arguments?.let {
letterId = it.getString(LETTER).toString()
}
}
Because it’s possible for arguments
to be optional, we will call let()
and pass in a lambda. This code will execute assuming arguments
is not null, passing in the non-null arguments for the it
parameter. If arguments
is null
, however, the lambda will not execute.
- Finally, we can access the
letterId
when we set the recycler view's adapter. Replaceactivity?.intent?.extras?.getString(LETTER).toString()
inonViewCreated()
withletterId
.
recyclerView.adapter = WordAdapter(letterId, requireContext())
Now we will run the app. It’s now able to navigate between two screens, without any intents, and all in a single activity.
That is all for Day61 ✅
Thanks for reading, See you tomorrow!
If you are reading my #100Days Journey, feel free to drop by ;)