To ensure the correctness of navigation, Modo requires the use of built-in functions for integration. You have two approaches to integrate Modo into your app:
rememberRootScreen - a convenient integration for Activity or Fragment. Use it inside the setContent function.
Modo.getOrCreateRootScreen, Modo.save, and Modo.onRootScreenFinished - for manual integration.
Convenient Integration for Activity and Fragment
rememberRootScreen is an easy way to integrate Modo into your Activity or Fragment. It automatically handles the saving and restoring of RootScreen during the Activity lifecycle and process death.
To use Modo inside your Activity or Fragment, follow these steps:
Use rememberRootScreen inside the setContent function and pass the screen you want to render. This will give you an instance of the RootScreen for further display.
Display content by calling fun Content(modifier: Modifier) on the created instance of RootScreen.
class QuickStartActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// Remember root screen using rememberSeaveable under the hood.
val rootScreen = rememberRootScreen {
DefaultStackScreen(StackNavModel(SampleScreen(screenIndex = 1)))
}
rootScreen.Content(modifier = Modifier.fillMaxSize())
}
}
}
If you prefer more control over Modo integration, you can use Modo.getOrCreateRootScreen, Modo.save, and Modo.onRootScreenFinished. Check out the ModoLegacyIntegrationActivity in the sample project for an example.
Modo.getOrCreateRootScreen - initializes RootScreen with the provided screen or returns the existing instance.
Modo.save - saves the current state of RootScreen and other internal data (like screenCounterKey) to the bundle for future restoration in Modo.getOrCreateRootScreen.
Modo.onRootScreenFinished - should be called when RootScreen is no longer needed, such as when the Activity or Fragment finishes. It removes the instance of RootScreen from the internal runtime cache and clears other internal resources.
class ModoManualIntegrationActivity : AppCompatActivity() {
private var rootScreen: RootScreen<StackScreen>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
rootScreen = Modo.getOrCreateRootScreen(savedInstanceState, rootScreen) {
SampleStack(MainScreen(1))
}
setContent {
ActivityContent {
rootScreen?.Content(Modifier.fillMaxSize())
}
}
}
override fun onSaveInstanceState(outState: Bundle) {
Modo.save(outState, rootScreen)
super.onSaveInstanceState(outState)
}
override fun onDestroy() {
super.onDestroy()
if (isFinishing) {
Modo.onRootScreenFinished(rootScreen)
}
}
}