Android Development Android Performance Optimization 2 — Questions and Answers
Question 1: What is the purpose of the RecyclerView.RecycledViewPool in Android?
- To cache network responses for list items
- To share recycled ViewHolders across multiple RecyclerViews (Correct answer)
- To limit the number of views that can be inflated
- To persist scroll position across configuration changes
Correct answer: To share recycled ViewHolders across multiple RecyclerViews
RecycledViewPool allows multiple RecyclerViews to share a pool of recycled ViewHolders, reducing view inflation overhead when displaying similar list content.
Question 2: What does 'overdraw' mean in the context of Android rendering performance?
- Drawing too many items in a RecyclerView
- A pixel being painted more than once per frame (Correct answer)
- Allocating too many objects in a tight loop
- Exceeding the maximum texture size on the GPU
Correct answer: A pixel being painted more than once per frame
Overdraw occurs when a pixel is drawn multiple times in a single frame (e.g., due to overlapping backgrounds), wasting GPU time and reducing rendering performance.
Question 3: Which Android feature allows deferring the inflation of part of a layout until it is actually needed, improving initial load time?
- ConstraintLayout chains
- ViewStub (Correct answer)
- Data Binding
- MotionLayout
Correct answer: ViewStub
ViewStub is a lightweight, invisible View that is replaced by the inflated layout only when explicitly made visible or inflated, reducing startup inflation cost.
Question 4: What is the main benefit of using ConstraintLayout over nested LinearLayouts or RelativeLayouts?
- It supports hardware acceleration by default
- It flattens the view hierarchy, reducing measure/layout passes (Correct answer)
- It automatically caches view positions
- It removes the need for ViewHolders in RecyclerView
Correct answer: It flattens the view hierarchy, reducing measure/layout passes
ConstraintLayout expresses complex layouts in a single flat hierarchy, avoiding nested layouts that require multiple measure/layout passes and hurt rendering performance.
Question 5: When using Room database, what should you avoid doing on the main thread to prevent ANR errors?
- Defining DAO interfaces
- Running database queries synchronously (Correct answer)
- Annotating entity classes
- Using LiveData return types
Correct answer: Running database queries synchronously
Room by default throws an exception if database queries are run on the main thread; they should be executed on a background thread (e.g., using coroutines or LiveData) to prevent blocking the UI.
Question 6: What is 'baseline profiles' in Android, and what is their primary benefit?
- A set of user permissions required at install time
- Pre-compiled bytecode rules that speed up app startup and frame rendering (Correct answer)
- Memory allocation profiles stored per-user
- GPU shader caches bundled with the APK
Correct answer: Pre-compiled bytecode rules that speed up app startup and frame rendering
Baseline Profiles provide pre-compilation hints that let ART ahead-of-time compile critical code paths, significantly improving startup time and reducing jank during early app use.
Question 7: Which Jetpack library provides built-in support for pagination, loading large data sets from a data source in chunks to improve performance?
- WorkManager
- Paging 3 (Correct answer)
- DataStore
- Navigation
Correct answer: Paging 3
The Paging 3 library handles loading data in pages from network or database sources, integrating with RecyclerView and LiveData/Flow to display large datasets efficiently.
What is the purpose of the RecyclerView.RecycledViewPool in Android?