Mastering states in android compose.

Shailesh Mishra
3 min readAug 22, 2023
shaileshmishra

Mastering states in Android Compose involves understanding how to manage and display different UI states, such as loading, error, and success, using the Compose framework. Here’s a detailed implementation guideline and training to help you achieve this:

Prerequisites:
- Familiarity with Kotlin programming language.
- Basic understanding of Android Compose framework.

Step 1: Set Up Your Project
1. Create a new Android project in Android Studio.
2. Update your `build.gradle` files to include the necessary dependencies for Compose. As of my last update in September 2021, Compose was in beta, so make sure to check for the latest version.


// Project-level build.gradle
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21" // or the latest Kotlin version
// …
}

// App-level build.gradle
plugins {
id 'com.android.application'
id 'kotlin-android'
// …
}
android {
// …
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion "1.0.4" // or the latest Compose version
}
}

dependencies {
implementation "androidx.core:core-ktx:1.6.0" // for Kotlin extensions
implementation "androidx.activity:activity-compose:1.4.0" // for Jetpack Compose integration
// …
}

Step 2: Define UI States
1. Create a sealed class to represent different UI states (Loading, Success, Error) in a new or existing Kotlin file.

sealed class UiState<out T> {
object Loading : UiState<Nothing>()
data class Success<T>(val data: T) : UiState<T>()
data class Error(val message: String) : UiState<Nothing>()
}

Step 3: Create a Composable Function for Each State
1. For each UI state, create a composable function that takes a corresponding UI state object as a parameter and displays the appropriate UI.


@Composable
fun LoadingScreen() {
// Display loading UI
}
@Composable
fun SuccessScreen(data: DataType) {
// Display success UI with the provided data
}
@Composable
fun ErrorScreen(errorMessage: String) {
// Display error UI with the provided error message
}

Step 4: Create a Main Composable Function
1. Create a main composable function that observes the UI state and displays the appropriate screen based on the current state.


@Composable
fun MainScreen(uiState: UiState<DataType>) {
when (uiState) {
is UiState.Loading -> LoadingScreen()
is UiState.Success -> SuccessScreen(uiState.data)
is UiState.Error -> ErrorScreen(uiState.message)
}
}

Step 5: Use ViewModel to Manage State
1. Create a ViewModel to manage your UI state using the `ViewModel` class from the Android Architecture Components.


class MyViewModel : ViewModel() {
private val _uiState = mutableStateOf<UiState<DataType>>(UiState.Loading)
val uiState: State<UiState<DataType>> = _uiState
// Implement functions to update _uiState based on your app logic
}

Step 6: Connect ViewModel to Composable
1. Use the `viewModel` composition local to access the ViewModel in your composable function.


@Composable
fun MyScreen(viewModel: MyViewModel = viewModel()) {
val uiState by viewModel.uiState
MainScreen(uiState)
}

c Step 7: Put It All Together
1. Create your main activity and set the content to your `MyScreen` composable.


class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyScreen()
}
}
}

Step 8: Testing Different States
1. In your ViewModel, update the _uiState value based on different scenarios (loading, success, error).

Training Resources:
- Jetpack Compose Documentation — The official documentation is a great place to start and covers the fundamental concepts.
- Compose Academy — Offers in-depth tutorials and courses on different aspects of Jetpack Compose.

--

--