More in Mobile Dev — page 27

What is an Android memory leak? Give a Context example and fix.
Tests understanding of Activity Context retention preventing GC. A strong answer defines a leak as unreachable objects, gives a static singleton example, names LeakCanary, and fixes it with Application Context. Red flag: blaming GC or suggesting System.gc().

What is Android Test Orchestrator and how does it isolate tests?
Tests process-level isolation in Android UI suites. A strong answer says it runs each test in its own Instrumentation process, stopping crashes or leaked state from cascading, and notes slower startup.

What is the role of TestDispatcher and runTest in coroutine testing?
This tests coroutine test infrastructure and virtual time control. A great answer says runTest installs a TestDispatcher and TestScope, skips delays automatically, and exposes advanceTimeBy to test timeouts instantly.

How would you implement and register a custom IdlingResource?
WHAT IT TESTS: Whether you know Espresso's async contract. ANSWER OUTLINE: Implement the three IdlingResource methods with an atomic counter; register via IdlingRegistry in setup and unregister in teardown. RED FLAG: Suggesting Thread.sleep or polling loops.

How do you test a Compose click and verify state change?
This tests Compose semantics and interaction-to-assertion flow. Mention ComposeTestRule, onNodeWithTag, performClick, and assert on the changed node with assertIsDisplayed or assertTextEquals.

Mock vs Fake test doubles: when to use a Fake Repository?
Tests if you know a Fake is a working lightweight implementation while a Mock verifies interactions. Good answers define Fake as in-memory with real logic, Mock as expectation-based, and choose a Repository Fake when many tests need reusable state.

Explain integration tests and isolate Room DAO tests
Tests integration vs unit testing and Room isolation. Outline: verify real DAO-to-DB interaction with Room.inMemoryDatabaseBuilder; reset state by closing and recreating the DB in @After. Red flag: mocking the DAO or using an on-disk database.

Unit test a ViewModel exposing StateFlow in a local JVM test
Tests coroutine hygiene and flow collection strategy. A strong answer covers injecting a TestDispatcher, using runTest, collecting emissions in a background scope, and asserting states.

Why mock a Repository dependency in a ViewModel unit test?
This tests your grasp of test isolation. A great answer says mocking eliminates real database or network dependencies so you can verify ViewModel state changes in isolation. A red flag is insisting on using the real Repository inside the unit test.

What is the difference between test and androidTest source sets?
This tests understanding of test environments. OUTLINE: Say test runs on the JVM for unit tests mocking Android classes, while androidTest runs on device for instrumented tests. RED FLAG: Saying both need a device or treating them interchangeably.

Android 12 foreground service restrictions and WorkManager expedited jobs
Tests Android 12 background-start limits. A great answer covers: the API 31 ban on background FGS starts, BOOT_COMPLETED exceptions, and WorkManager setExpedited with out-of-quota fallback. Red flag: starting an FGS in a BroadcastReceiver ignoring quota.

Explain Structured Concurrency in Kotlin Coroutines
WHAT IT TESTS: your grasp of Kotlin coroutine parent-child scope hierarchies. ANSWER OUTLINE: structured concurrency binds coroutines to a scope; parent cancellation propagates to all children, preventing leaks. RED FLAG: calling them unmanaged threads.

When would you choose Foreground Service over WorkManager?
Tests immediate user-visible work versus deferrable jobs. Strong answers cite a notification-driven use case the user actively expects, like live navigation or a workout. Red flag: claiming WorkManager replaces real-time foreground tasks.

What is the difference between viewModelScope and lifecycleScope?
This tests scope ownership across configuration changes. viewModelScope survives rotation because it lives in ViewModel, while lifecycleScope dies with UI; use former for logic and latter for UI tasks. Never launch data loads in lifecycleScope.

How do you make a network request in an Activity using coroutines?
This tests main thread safety and coroutine dispatchers. A strong answer cites NetworkOnMainThreadException, uses lifecycleScope, and switches to Dispatchers.IO. Red flag: suggesting Dispatchers.Main for the network call or omitting the exception entirely.

Replace a production NetworkService with a fake in Hilt instrumented tests
Tests whether you know Hilt test overrides beyond basic DI. A strong answer covers @BindValue for field-level fakes and @TestInstallIn or @UninstallModules for module swaps.

How would you access a Hilt dependency in a non-injectable ContentProvider?
Tests Hilt's escape hatch for framework classes. Strong answer: define an EntryPoint in SingletonComponent, expose the dependency, and retrieve it with EntryPointAccessors.fromApplication. Red flag: field injection or manual static singletons.

How do you inject two different OkHttpClient instances with Hilt?
Tests Hilt qualifiers for same-type bindings. Answer: define a custom @Qualifier (or @Named), annotate two @Provides methods and the injection site. Red flag: manual client creation or subclassing OkHttpClient instead of qualifying the binding.

Explain Hilt scoping: @Singleton vs @ActivityRetainedScoped
Tests Hilt component lifecycles. Answer: @Singleton lives for the app process; @ActivityRetainedScoped survives config changes via ActivityRetainedComponent but dies when the activity finishes.

How do you inject UserRepository into a ViewModel with Hilt?
Tests Hilt constructor injection for classes you own. Annotate UserRepository's constructor with @Inject so Hilt auto-provides it to consumers like a @HiltViewModel. Red flag: using a @Provides module for a class you control.