Android Views Flashcards
An Overview About View Lifecycle
Every Activity has it’s own life cycle similarly Views also have a Life Cycle. A view which was rendered on the screen must undergo these lifecycle methods to get drawn on the screen correctly. Each of these methods has its importance. Let’s dive into the life cycle.
1 st Cycle:
1.Constructor->2.onAttachedToWindow()->3.measure()->4.onMeasure()->5.layout()->6.onLayout()->7.dispatchToDraw()->8.draw()->9.onDraw()->10.VisibleToUser
2nd Cycle:
9. onDraw()->11.invalidate()->6.dispatchToDraw()-> repeat from next 6 to 9 or 10.
3rd cycle:
9.onDraw()->12.requestLayout()->3.measure() -> repeat from next 4 to 9 or 10.
Methods Descriptions:
- onAttachedToWindow() Called when the view is attached to a window. View class have its opposite method onDettachToWindow()
- onMeasure(int, int) Called to determine the size requirements for this view and all of its children. But we don’t use it. we use setMeasuredDimension(800, 300);
- onLayout( int, int, int, int) Called when this view should assign a size and position to all of its children.
- onDraw(android.graphics.Canvas) Called when the view should render its content. It provides canvas as an argument, we draw anything on canvas using Paint class Instance.
What is the difference between invalidate & request layout?
Invalidate says something in your visuals has changed so you need to be redrawn; requestLayout() says something structural has changed so it needs to recalculate the measure and implement the layout phase which involves measure->layout->draw stages.
View Binding vs Data Binding
View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module.
The Data Binding Library is an Android Jetpack library that allows you to bind UI components in your XML layouts to data sources in your app using a declarative format rather than programmatically, reducing boilerplate code.
View binding: Only binding views to code
Data binding: Binding data(from code) to views + ViewBinding(binding views to code)
Few differences:
- With view binding, the layouts do not need a layout tag
- You can’t use viewbinding to bind layouts with data in xml (No binding expressions, no Binding Adapters nor two-way binding with viewbinding)
- The main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with databinding due to annotation processors affecting databinding’s build time.
Why data binding ?
In Android Development we are using the findViewById() function to obtain references to views. But, every time we do so , Android system has to go through the view hierarchy and find it at runtime. Moreover , there can be many layouts and hundreds of views in a larger Android application.
Therefore, system will have to go through the view hierarchy again and again . That is a very inefficient and resource consuming process.
So, Google introduced Android data binding library as a solution for that problem. It generates a corresponding binding class for each xml layout.
After that, we can easily use an instance(object) of that binding class to set values to and get values from view components. Eliminating findViewById() is not the only benefit of data binding. Compare to previous approaches data binding has many advantages.
Advantages of Data binding:
- Eliminate findViewById()
- Update the values automatically.(doesn’t have to keep track of all the ways a value can be updated)
- Very effective for UI Testing.
- More readable code.
- More maintainable code.
- Faster development times.
- Faster execution times.
- Errors can be found during the compile time.
- Well suited for MVVM and MVI architectures.