Day56 of #100DaysOfCode
Hii folks 🙌
Today I will be continuing the same pathway in which I will build the Dessert Clicker app.
Unit 3: Navigation
Pathway 2: Introduction to the Navigation component
Source: https://developer.android.com/courses/android-basics-kotlin/course
Fragments and the Navigation Component
We will start by downloading the repo from the given link in the course and setting it up in Android Studio.
A fragment is simply a reusable piece of our app’s user interface. The fragment's lifecycle state will be as follows:
- INITIALIZED: A new instance of the fragment has been instantiated.
- CREATED: The first fragment lifecycle methods are called. During this state, the view associated with the fragment is also created.
- STARTED: The fragment is visible onscreen but does not have “focus”, meaning it can’t respond to user input.
- RESUMED: The fragment is visible and has focus.
- DESTROYED: The fragment object has been de-instantiated.
Similar to activities, the Fragment
class provides many methods that we can override to respond to lifecycle events.
onCreate()
: The fragment has been instantiated and is in theCREATED
state. However, its corresponding view has not been created yet.onCreateView()
: This method is where we inflate the layout. The fragment has entered theCREATED
state.onViewCreated()
: This is called after the view is created. In this method, we would typically bind specific views to properties by callingfindViewById()
.onStart()
: The fragment has entered theSTARTED
state.onResume()
: The fragment has entered theRESUMED
state and now has focus (can respond to user input).onPause()
: The fragment has re-entered theSTARTED
state. The UI is visible to the useronStop()
: The fragment has re-entered theCREATED
state. The object is instantiated but is no longer presented on screen.onDestroyView()
: Called right before the fragment enters theDESTROYED
state. The view has already been removed from memory, but the fragment object still exists.onDestroy()
: The fragment enters theDESTROYED
state.
The lifecycle states and callback methods are quite similar to those used for activities. However, the difference with the onCreate()
method. With activities, we would use this method to inflate the layout and bind views. However, the fragment lifecycle onCreate()
is called before the view is created, so we can't inflate the layout here. Instead, we do this in onCreateView()
. Then, after the view has been created, the onViewCreated()
method is called, where we can then bind properties to specific views.
That is all for Day56 ✅
Thanks for reading, See you tomorrow!
If you are reading my #100Days Journey, feel free to drop by ;)