Guia Navigation
Github
  • Overview
  • Setup
  • Using Guia
    • 📔Express Lore
      • Quick Start
    • 📖The Lore
      • Navigation Key
      • Navigation Node
        • Screen
        • Bottom Sheet
        • Dialog
      • Navigator
        • Navigation Operations
        • Navigator Instance
        • Back Handling
      • Navigator Config
        • Dynamic Navigation Node
        • Transitions
      • Containers
      • NavHost
        • Back Handling
      • Multi module navigation
        • NavHost Multi module
      • Deep Linking / Global Navigation
      • Results
      • Lifecycle, ViewModel, Saved State
      • UI Tests
  • 🤖Advanced
    • Custom LifecycleManager
Powered by GitBook
On this page
  • Survive state restoration
  • Passing data between different navigators
  1. Using Guia
  2. The Lore

Results

PreviousDeep Linking / Global NavigationNextLifecycle, ViewModel, Saved State

Last updated 2 years ago

Passing results between navigation keys is done in Guia using the ResultManager API, it's type safe and stateful.

interface ResultManager {
    fun result(key: String): Any?
    fun setResult(key: String, result: Any)
    fun clearResult(key: String)
}

A itself is a ResultManager backed by a stateful key/value map.

data class Result(val item: String)

@Composable
fun HomeScreen() {
    val navigator = requireLocalNavigator()
    val result = navigator.result<Result>()
    
    Column {
        Text(text = "Result: $result")
        
        Button(onClick = { 
            navigator.setResult<Result>(UUID.randomUUID().toString) 
        }) {
            Text(text = "Refresh Result")
        }
        
        Button(onClick = { 
            navigator.clearResult<Result>() 
        }) {
            Text(text = "Clear Result")
        }
    }
}

@Composable
fun AnotherScreen() {
    val navigator = requireLocalNavigator()
    
    Button(onClick = { 
        navigator.setResult<Result>(UUID.randomUUID().toString()) 
        navigator.pop()
    }) {
        Text(text = "Return Result")
    }
}

Survive state restoration

By simply marking our result as Parcelable our result can now be saved and restored, the ResultManager will handle that internally.

@Parcelize
data class Result(val item: String): Parcelable

Passing data between different navigators

The ResultManager API is public can be used to create our own:

val resultManager = rememberResultManager()

Now we can provide this result manager as CompositionLocal to children Composables, that can potentially be hosting different navigators.

val LocalParentResultManager = compositionLocalOf<ResultManager> { error() }

@Composable
fun ParentScreen() { // That hosts multiple navigators
    val resultManager = rememberResultManager()

    CompositionLocalProvider(
        LocalParentResultManager provides resultManager
    ) {
        // Nested navigation
    }
}

@Composable
fun SomewhereNested() {
    val parentResultManager = LocalParentResultManager.current
    // Do stuff
}

📖
Navigator