Application lifecycle and integration points
Metrolist is an Android music app whose UI, playback, recognition, widgets, alarms, and external links meet at a small set of Android entry points. The application sets up shared state, MainActivity owns the user-facing navigation, and services or receivers keep work active when no screen is visible.
Parent: Application platform
Related: Android build variants
Lifecycle model
Process initialization
The application process starts through App, which prepares shared app state before screens or long-running work use it. This includes crash handling, shared settings, network-facing session state, image loading, logging, and background warm-up. The crash screen uses a separate process, so App avoids starting the normal app setup there.
The main process starts its work in this order:
- Install process-wide handlers and shared helpers.
- Read settings that affect language, content access, and network access.
- Start background observers for settings and session state.
- Allow activities and services to obtain the shared dependencies from the application scope.
References: app/…/App.kt• L61-117 app/…/App.kt• L119-194 app/…/App.kt• L196-293
Shared dependency graph
Dependency injection supplies app-wide resources to activities, view models, and services. Hilt creates one shared graph for resources such as the database, application coroutine scope, media caches, network observer, and Listen Together manager. Components that run outside the normal activity path can use the same graph when they have no direct activity to receive dependencies from.
This gives the main integration points a common view of stored data and active sessions:
| Consumer | Shared resource role |
|---|---|
MainActivity | Reads app data and controls the visible experience. |
MusicService | Runs playback and exposes player state to Android media clients. |
| Recognition service | Runs microphone recognition and records results. |
| View models | Coordinate screen-specific data without owning app-wide resources. |
References: app/…/AppModule.kt• L32-109 app/…/NetworkModule.kt• L17-25 app/…/App.kt• L61-67
Activity lifecycle
Main activity
MainActivity is the single task entry point for the primary app experience. It creates the Compose content and navigation controller, connects the visible UI to the playback service, and turns incoming Android intents into app routes or playback actions.
The activity keeps playback integration alive while the app is in the background. It starts the playback service when needed, binds to it for a PlayerConnection, and releases the connection when the activity is actually destroyed. This separates the visible screen lifecycle from music playback, so playback can continue without an open screen.
References: app/…/MainActivity.kt• L239-311 app/…/MainActivity.kt• L314-379 app/…/MainActivity.kt• L394-481
Auxiliary activities
The manifest adds small activities for flows that need a temporary Android entry point rather than a full app screen. They finish after handing control to the right owner.
| Activity | Lifecycle role |
|---|---|
RecognitionLaunchActivity | Checks microphone access and starts recognition or opens the recognition permission path in MainActivity. |
DiscordOAuthActivity | Receives the browser callback and returns the authorization result to the waiting integration flow. |
CrashActivity | Shows a saved crash report in its isolated process and lets the user close or share it. |
References: app/…/RecognitionLaunchActivity.kt• L12-57 app/…/DiscordOAuthActivity.kt• L13-76 app/…/CrashActivity.kt• L53-107 app/…/AndroidManifest.xml• L49-62 app/…/AndroidManifest.xml• L193-231
Navigation model
Navigation graph
The Compose navigation graph maps stable route names to user-facing screens. MainActivity creates the navigation host and passes shared UI state into navigationBuilder, which defines the main tabs and detail, account, settings, equalizer, and recognition destinations.
The graph uses route arguments for content that depends on an identifier, such as an album, artist, playlist, search query, or recognition mode. Screens navigate by route so that the same destination can be reached from the app UI, widgets, and external links.
The main navigation items are Home, Search, Listen Together, and Library. The app presents them as a bottom bar on narrow layouts and as a rail on wide or landscape layouts. Child routes remain selected under their related main item.
References: app/…/MainActivity.kt• L726-781 app/…/MainActivity.kt• L1330-1400 app/…/NavigationBuilder.kt• L68-149 app/…/NavigationBuilder.kt• L347-455 app/…/Screens.kt• L13-50 app/…/AppNavigation.kt• L36-48
Intent routing
Incoming intents enter through MainActivity after the navigation controller exists. The activity also stores an intent that arrives before Compose has finished creating the controller, then processes it after initialization. Because the manifest uses singleTask, later intents arrive through onNewIntent instead of creating another main activity.
The routing layer handles three broad sources:
- Recognition actions open the recognition screen and can request automatic recognition.
- Widget target actions open a local, online, or generated playlist route.
- Web links, shared text, media search actions, and Listen Together links open content, start playback, or join a room.
The handlers clear consumed intent data. This prevents the same external action from running again during a later lifecycle callback.
The following diagram shows how an Android entry point becomes either a screen route, a playback action, or a room join.
flowchart LR
A[Android intent] --> B{Intent type}
B -->|Recognition| C[Recognition route]
B -->|Widget target| D[Playlist route]
B -->|Web link or shared text| E{Content}
E -->|Listen Together| F[Join room]
E -->|Music content| G[Open route or play queue]
C --> H[Compose navigation host]
D --> H
G --> H
References: app/…/MainActivity.kt• L382-389 app/…/MainActivity.kt• L988-1011 app/…/MainActivity.kt• L1533-1701 app/…/AndroidManifest.xml• L56-159
Android service entry points
Playback service
MusicService is the long-lived playback boundary. It owns the player session and exposes media controls to the notification, Android media clients, Android Auto, widgets, and the app activity. MainActivity uses its binder for direct UI access, while external Android clients use the declared media service interface.
The service starts as a media playback foreground service before it performs slower setup. It then restores playback state, creates the media session, and watches shared settings and device state. This lets audio continue when the activity is stopped and gives Android a standard media control surface.
References: app/…/MusicService.kt• L263-332 app/…/MusicService.kt• L606-747 app/…/AndroidManifest.xml• L233-253
Recognition service
RecognitionForegroundService is the microphone work boundary. A short launch activity or a widget starts it, and the service reports listening, processing, success, no-match, or error state through its notification and widget state.
Recognition results can be saved for later display and can return to MainActivity through a pending intent. The service is not bound to a screen, so recognition can finish while the app UI is not active.
References: app/…/RecognitionForegroundService.kt• L57-107 app/…/RecognitionForegroundService.kt• L178-279 app/…/RecognitionForegroundService.kt• L488-506 app/…/AndroidManifest.xml• L361-375
Receiver entry points
Alarm receivers
Alarm receivers connect Android system time events to scheduled playback. The trigger receiver forwards the saved alarm target to MusicService, while the reschedule receiver rebuilds alarms after boot, time changes, time-zone changes, or app replacement.
These receivers perform only the handoff that must happen at the system boundary. They use asynchronous receiver completion so Android knows when the reschedule work is done.
References: app/…/MusicAlarmReceiver.kt• L12-36 app/…/MusicAlarmRescheduleReceiver.kt• L10-29 app/…/AndroidManifest.xml• L260-277
Widget receivers
Widget receivers connect home-screen controls to the playback service and connect widget targets to app navigation. A widget update refreshes from the running playback service when possible. A user action can start the service and send a playback command, while a target action opens a route in MainActivity.
The recognizer widget uses the recognition foreground service for microphone work. Its state is stored outside the activity so the widget can show progress and results while the app is closed.
References: app/…/MusicWidgetReceiver.kt• L16-88 app/…/RecognitionForegroundService.kt• L281-393 app/…/AndroidManifest.xml• L290-359
Change checklist
When these files or sections are changed, remember to consider these:
- Keep every externally handled intent action and URI shape aligned between AndroidManifest.xml,
MainActivityrouting, and the sender that creates the intent. - Keep route argument names aligned between
NavigationBuilder, route builders, deep-link parsing, and widget target mapping. - Keep foreground-service types, permissions, and start paths aligned with the work done by each service.
- Keep
MusicServicebinder use, media-session declarations, and activity bind/unbind behavior aligned so background playback and screen controls do not diverge. - Keep receiver boot and time-change actions aligned with alarm rescheduling and the permission needed for scheduled alarms.
- Keep Hilt entry points aligned for components that run without an activity, especially recognition and media services.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A new external action is added without a manifest filter or handler. | Android can launch the app without opening the intended screen or action. | Update the manifest and the matching MainActivity intent path together. |
A foreground service starts too late after startForegroundService. | Android can stop the service before playback or recognition starts. | Promote the service to the foreground before slow setup. |
| The activity consumes an intent before navigation exists. | A widget target or deep link is lost. | Store the pending intent and process it after the navigation host is ready. |
| A route argument changes in only one producer or destination. | Navigation reaches a screen with missing or invalid content. | Treat route templates and argument names as one contract. |
| A receiver starts work without completing its async result. | Android may stop the receiver before alarm state is restored. | Use the receiver's pending result until rescheduling finishes. |
| The crash process runs normal application setup. | The crash screen can conflict with WebView data or fail to start. | Keep main-process initialization out of the crash process. |
Key files
| File | Why to read it |
|---|---|
| app/…/App.kt | Application-wide setup and process boundary. |
| app/…/MainActivity.kt | Main activity lifecycle, service connection, navigation host, and intent routing. |
| app/…/NavigationBuilder.kt | Route-to-screen map and route argument contracts. |
| app/…/Screens.kt | Main navigation destinations and their stable route names. |
| app/…/MusicService.kt | Playback service and Android media integration boundary. |
| app/…/RecognitionForegroundService.kt | Recognition service lifecycle and result handoff. |
| app/…/MusicAlarmReceiver.kt | Alarm-to-playback handoff. |
| app/…/MusicAlarmRescheduleReceiver.kt | System-event alarm restoration. |
| app/…/AndroidManifest.xml | Android entry-point declarations, permissions, filters, and service types. |
| app/…/AppModule.kt | Shared dependency graph and app-scoped resources. |