Develop

Achievements sample overview

Updated: May 8, 2026

Overview

This sample demonstrates how to initialize the Horizon Service and instantiate the Achievements service object for Meta Horizon Platform SDK integration in a Kotlin-based Android application. The sample is a minimal stub that focuses exclusively on service connection patterns and does not demonstrate achievement operations such as unlocking, querying definitions, or tracking progress.
Scope note: This sample covers service initialization only. For achievement operations such as unlocking, querying definitions, and tracking progress, see the Horizon Platform SDK documentation. View the sample source code on GitHub.

Learning objectives

Complete this guide to learn how to:
  • Connect to Horizon Service by using HorizonServiceConnection.connect()
  • Instantiate an Achievements service object
  • Use MVVM architecture patterns with Jetpack Compose and StateFlow
  • Implement error handling for SDK initialization failures
  • Unit test UI state management

Requirements

  • Development environment: Android Studio with Kotlin 2.0 support
  • Target device: Meta Quest device with Android API 34+
  • Platform SDK setup: Complete the Horizon Platform SDK setup for Kotlin before running this sample

Get started

Clone or download the sample from the Horizon Platform SDK samples repository and open the achievements/ directory in Android Studio. Before exploring the code on-device, replace the APPLICATION_ID placeholder in MainActivity.kt with your application ID from the Meta Developer Dashboard — skipping this step causes the app to crash with IllegalStateException. For complete dependency and build information, see the sample’s README.

Explore the sample

FileWhat it demonstratesKey concepts
MainActivity.kt
SDK initialization via HorizonServiceConnection.connect(), Compose UI entry point
Application ID configuration, lifecycle scope integration, exception handling for missing credentials
AchievementsViewModel.kt
Achievements() service instantiation, initialization verification, UI state management
No-arg constructor pattern, StateFlow-based tri-state loading pattern (loading/success/error), coroutine-based initialization check
AchievementsViewModelTest.kt
Unit tests for AchievementsUiState data class
Data class property validation, default state testing
app/build.gradle.kts
SDK dependency configuration
Artifact declaration for com.meta.horizon.platform.sdk:achievements-kotlin:0.2.0

Runtime behavior

When you launch the app, you see a title Achievements Sample with description text and a single Check Initialization button below. When you tap the button, the app shows a brief loading spinner, then displays “Achievements service initialized successfully.” in a card. If initialization fails, an error card appears instead.

Key concepts

Horizon Service connection

The sample connects to the Horizon Service by calling HorizonServiceConnection.connect() with three parameters: application ID, application context, and a coroutine scope (typically lifecycleScope). SDK service objects require a successful connection before they can be used.
HorizonServiceConnection.connect(APPLICATION_ID, applicationContext, lifecycleScope)
See MainActivity.kt for the complete implementation.

Achievements service instantiation

The Achievements service uses a no-arg constructor. The sample instantiates it as a ViewModel property and verifies initialization by calling toString() on the object.
private val achievements = Achievements()
See AchievementsViewModel.kt for the complete implementation.

MVVM with StateFlow

The sample implements a tri-state UI pattern using AchievementsUiState, a data class with isLoading, resultMessage, and errorMessage properties. The ViewModel exposes state via StateFlow, and the composable UI collects it using collectAsStateWithLifecycle().
data class AchievementsUiState(
    val isLoading: Boolean = false,
    val resultMessage: String? = null,
    val errorMessage: String? = null,
)
See AchievementsViewModel.kt for the complete implementation.

Extend the sample

  • Add achievement operations by integrating SDK methods for querying achievement definitions, unlocking achievements, and tracking progress.
  • Combine with the Leaderboards sample to create a full gamification experience.
  • Implement UI to display achievement metadata, including names, descriptions, and unlock status.