Basic lifecycle of a view in android.

Abhinay Gupta
2 min readJun 9, 2023

--

The basic lifecycle of a View in Android can be summarized in the following steps:

1. Instantiation:
— The View is created either programmatically or through XML layout inflation.

2. Initialization:
— The View’s constructors are called, and any initial setup or customization can be performed.
— Attributes defined in XML layout files are applied to the View.

3. Measuring:
— The `onMeasure()` method is called to determine the View’s size requirements based on its layout parameters and content.
— The View specifies its desired width and height by calling `setMeasuredDimension()`.

4. Layout:
— The parent ViewGroup determines the position and size of each child View.
— The `onLayout()` method is called on the parent ViewGroup, which in turn calls `layout()` on each child View to set its position.

5. Drawing:
— The `onDraw()` method is called, allowing the View to perform custom drawing by using the Canvas object.
— The View’s appearance is rendered on the screen.

6. User Interaction:
— The View starts to interact with the user by handling touch events, gestures, or other input events.
— The View may respond to user actions by triggering changes or invoking callbacks.

7. Visibility Changes:
— The View’s visibility can change, such as when it becomes visible or hidden.
— The `onVisibilityChanged()` method is called to notify the View about visibility changes.

8. State Changes:
— The View’s state can change due to user interaction, data updates, or other factors.
— The View’s state can be saved and restored using methods such as `onSaveInstanceState()` and `onRestoreInstanceState()`.

9. Destruction:
— The View can be destroyed when it is no longer needed, such as when the Activity or Fragment containing it is destroyed.
— The `onDetachedFromWindow()` method is called to indicate that the View is being removed from the window hierarchy.

It’s important to note that these lifecycle steps are often managed by the Android framework, and certain methods are automatically called at specific points in the lifecycle. Customization and handling of specific lifecycle events can be implemented by overriding the corresponding methods in a custom View subclass.

--

--

Abhinay Gupta

Exploring personal growth, tech, and culture through engaging storytelling | Inspiring and connecting with readers worldwide.