Val vs Var
Any programming language a variable is referred to a location in memory(storage area) to stored data. The type of variable defines the range of value that the variable can hold. So indicate a storage space, each variable should be given a unique identifier.
Basically, val and var both are used to declare a variable. var is like a general variable and can be assigned multiple times and is known as the mutable variable in Kotlin. Whereas val is a constant variable and can not be assigned multiple times and can be Initialized only single time and is known as the immutable variable in Kotlin.
Declaring a variable in Kotlin
Var
is short for variable – The object stored in the variable could change (vary) in time.
private val adapter: HomeAdapter? = null
After the Initialized It will not throw me an error because var is a mutable and it can be assigned multiple times.
Val
is short for value – The object stored in val, could not vary in time. Once assigned the val becomes read only, like a constant in Java Programming language.
private var adapter: HomeAdapter? = null
After the Initialized It will throw me an error like “Val can not be reassigned”
So to conclude we have learnt about Val vs Var in Kotlin that var could be changed at any level. Whereas val once assigned, could not be changed, but its properties could be changed.
- Published in Android, Kotlin, Programming Languages, Uncategorized
Android w/ Fragments
A fragment is an Android component that holds part of the behavior and/or UI of an activity. As the name would suggest, fragments are not independent entities, but are tied to a single activity. In many ways, they have functionality similar to activities.
In the same way that you don’t actually need an army of little helpers to do your bidding, you don’t have to use fragments. However, if you use them well, they can provide:
- Modularity – Diving complex activity code across fragments for better maintenance
- Adaptability – representing sections of a UI within the different fragments while utilizing different layouts that relys on the screen orientation and size
- Reusability – placing behavior or UI parts within the fragments that multiple activities can share
Fragment Lifecycle of Android
As you already know like activity, a fragment has a lifecycle with events that occur when the status changes within the framents. For example, when an event fires becasue a fragment becomes visible and active, or is not used or removed. Just as in a regular activity you are able to add code and behavior to the callbacks for the events.
Below you will see the fragment lifescycle diagram:

Understanding the lifecycle
- onAttach : the fragment attaches to its host activity
- onCreate : When a new fragment initializes, which always happens after it attaches to the hosts.
- onCreateView : creates its portion of the view hierarchy, which then added to the activity’s hierarchy of view
- onActivityCreated : fragment’s activity has finished on its own onCreate
- onStart : fragment becomes visible and starts after its activity starts
- onResume : fragment is visible and interactable that resumes only after its activity resumes and often resumes immediately after the activity does.
- onPause : user may not interact
- onStop : this is fired when the fragment is no longer visible; the fragment will get change with another fragment or it gets removed from actvity or fragment’s is stopped
- onDestroyView : this is fired when the view and other resources created within the onCreateView() are removed from the activity’s view hierarchy and destroyed.
- onDestroy : fired when the fragment does the final clean up.
- onDetach : fires when the fragment is detached from the activity host
If you want to learn more details regarding please read “Droid Fragments Life”.This is all about how the fragment come up within the activity and goes out. Cool!!
- Published in Android, Java, Kotlin, Programming Languages


