Day61 of #100DaysOfCode

#CodeTogether Day 61/100

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 a letterId property. We can mark this as lateinit so that we don't have to make it nullable.
private lateinit var letterId: String
  • Then override onCreate() (not onCreateView() or onViewCreated()!), 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. Replace activity?.intent?.extras?.getString(LETTER).toString() in onViewCreated() with letterId.
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 ;)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store