Compose Part 3
Q21. What tools do you use to measure Compose frame times and recomposition counts in production?
// Production jank tracking using JankStats
class JankMonitor(context: Context) {
private val jankStats = JankStats.createAndTrack(context.window) { frameData ->
if (frameData.isJank) {
// Report to analytics: frameData.frameDurationUiNanos / 1_000_000.0
Log.w("JankStats", "Jank detected: ${frameData.frameDurationUiNanos / 1_000_000.0}ms")
}
}
fun start() { jankStats.isTrackingEnabled = true }
fun stop() { jankStats.isTrackingEnabled = false }
}
// Macrobenchmark for CI/Pre-production
class ScrollBenchmark {
@get:Rule val benchmarkRule = MacrobenchmarkRule()
@Test
fun scrollList() = benchmarkRule.measureRepeated(
packageName = "com.example.app",
metrics = listOf(FrameTimingMetric(), CpuUsageMetric()),
iterations = 5,
setupBlock = { pressHome(); startActivityAndWait() }
) {
val list = device.findObject(By.res("lazy_list"))
list.setGestureMargin(device.displayWidth / 5)
list.fling(Direction.DOWN)
}
}
// Custom frame callback overlay (debug builds only)
val frameCallback = object : Choreographer.FrameCallback {
private var lastFrame = 0L
override fun doFrame(frameTimeNanos: Long) {
if (lastFrame != 0L) {
val diffMs = (frameTimeNanos - lastFrame) / 1_000_000.0
if (diffMs > 17) Log.d("FrameDrop", "Frame took ${diffMs}ms")
}
lastFrame = frameTimeNanos
Choreographer.getInstance().postFrameCallback(this)
}
}Q22. How would you build a custom layout using Layout composable? What are MeasurePolicy and IntrinsicMeasurable?
Layout composable? What are MeasurePolicy and IntrinsicMeasurable?Q23. What is the difference between SubcomposeLayout and the standard Layout composable?
SubcomposeLayout and the standard Layout composable?Q24. How do you create a custom Modifier using Modifier.Node versus the older Modifier.composed approach?
Modifier using Modifier.Node versus the older Modifier.composed approach?Q25. Explain how DrawScope and Canvas work in Compose — how do you handle touch events on custom drawn elements?
DrawScope and Canvas work in Compose — how do you handle touch events on custom drawn elements?Q26. How would you implement a staggered grid layout before LazyVerticalStaggeredGrid existed in Compose?
LazyVerticalStaggeredGrid existed in Compose?Q27. How does Compose Navigation differ from the Fragment-based Navigation Component in terms of lifecycle and back stack?
Q28. How do you handle nested navigation graphs with independent back stacks in Compose?
Q29. What are the challenges of using type-safe navigation with Compose Navigation, and how do you implement it?
Q30. How do you manage screen transitions and shared element transitions between destinations in Compose Navigation?
Last updated