Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Mlc ai Mlc llm MainActivity

From Leeroopedia
Revision as of 15:50, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Mlc_ai_Mlc_llm_MainActivity.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Android, UI, LLM Chat
Last Updated 2026-02-09 19:00 GMT

Overview

MainActivity is the primary Android activity for the MLCChat application, responsible for bootstrapping the Compose UI, handling runtime permissions, and managing image input from the gallery or camera for multimodal chat interactions.

Description

The MainActivity class extends ComponentActivity and serves as the entry point of the MLCChat Android application. On creation, it initializes an AppViewModel.ChatState instance, requests the necessary runtime permissions (camera, storage), and sets the Compose content to the application's navigation view wrapped in the MLCChatTheme.

The activity registers three activity result launchers:

  • pickImageLauncher -- Handles picking an image from the device gallery via ActivityResultContracts.GetContent(). When an image URI is returned, it creates a MessageData object with MessageRole.User and appends it to the chat state messages.
  • takePictureLauncher -- Handles capturing a photo via the device camera using ActivityResultContracts.TakePicture(). On success, the captured image URI is added to the chat state.
  • requestPermissionLauncher -- Handles the result of a multiple-permission request using ActivityResultContracts.RequestMultiplePermissions().

Permission handling is version-aware: on Android 13 (TIRAMISU) and above, it requests READ_MEDIA_IMAGES and CAMERA; on older versions, it requests READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE, and CAMERA.

Usage

This activity is the launch activity of the MLCChat Android application. It is declared in the Android manifest and is instantiated by the Android framework when the user opens the app. Other composables and view models interact with it to trigger image picking (pickImageFromGallery()) and photo capture (takePhoto()).

Code Reference

Source Location

Signature

class MainActivity : ComponentActivity() {
    var hasImage: Boolean
    lateinit var chatState: AppViewModel.ChatState

    override fun onCreate(savedInstanceState: Bundle?)
    fun pickImageFromGallery()
    fun takePhoto()
    private fun requestNeededPermissions()
}

Import

import ai.mlc.mlcchat.MainActivity

I/O Contract

Input Type Description
savedInstanceState Bundle? Standard Android saved instance state bundle passed to onCreate
Output / Side Effect Description
Compose UI rendered Sets content with NavView wrapped in MLCChatTheme inside a full-size Surface
Permissions requested Requests camera and storage permissions at runtime based on API level
Image URI appended On gallery pick or camera capture, a MessageData with the image URI is appended to chatState.messages
Method Purpose
pickImageFromGallery() Launches an image picker for image/* content
takePhoto() Creates a MediaStore entry with a timestamped filename and launches the camera to capture a JPEG image
requestNeededPermissions() Checks and requests runtime permissions for camera and media access, adapting to the device's Android API level

Usage Examples

// From a composable that has access to the activity instance:
val activity = context as MainActivity

// Pick an image from the gallery for multimodal chat
activity.pickImageFromGallery()

// Capture a photo using the camera
activity.takePhoto()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment