Modo is a simple and convenient state-base navigation library for Jetpack Compose.
Basic navigation | MultiScreen navigation | Nested navigation |
NavigationState
defines UI- Initial state is defined in constructor of
ContainerScreen
bynavModel: NavModel<State, Action>
- To update state, use
dispatch(action: Action)
onNavigationContainer
, or build-in extension functions for StackScreen and MultiScreen
- Initial state is defined in constructor of
- There are Screen and ContainerScreen
- ContainerScreen can contain and render child screens
- There are some build-in implementation of ContainerScreen like StackScreen and MultiScreen
- You can easily create custom
Action
by extendingAction
orReducerAction
,
The best way to lear the library is discovering sample application. You can add Modo into your project doing following steps
- Add gradle dependency. In your
build.gradle.kts
plugins { //... //for serialization screens id("kotlin-parcelize") } dependencies { implementation("com.github.terrakok:modo-compose:${latest_version}") }
- Create your first screen. Generally it's convenient to use
StackScreen
as the first screen in the hierarchy:@Parcelize class SampleStack( private val stackNavModel: StackNavModel ) : StackScreen(stackNavModel) { constructor(rootScreen: Screen) : this(StackNavModel(rootScreen)) @Composable override fun Content(modifier: Modifier) { // Render the last screen from the stack TopScreenContent(modifier) { modifier -> // Define the animation for changing screens SlideTransition(modifier) } } }
- Create simple screen
@Parcelize class SampleScreen( override val screenKey: ScreenKey = generateScreenKey() ) : Screen { @Composable override fun Content(modifier: Modifier) { Text(text = "Hello world", modifier = modifier.fillMaxSize()) } }
- Integrate screen with to your application. You can use
rememberRootScreen()
inside yourFragment
orActivity
:- For Activity
class MyActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { // Remember root screen using rememberSeaveable under the hood. val rootScreen = rememberRootScreen { SampleStack(MainScreen(1)) } rootScreen.Content(modifier = Modifier.fillMaxSize()) } } }
- For Fragment
class MyFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View = ComposeView(inflater.context).apply { setContent { val rootScreen = rememberRootScreen { SampleStack(MainScreen(screenIndex = 1, canOpenFragment = true)) } rootScreen.Content(modifier = Modifier.fillMaxSize()) } } }
- For Activity
Feature | Status | Source/Docs | Sample |
---|---|---|---|
Stack navigation | ✅ | StackScreen, build-in commands | SampleStack, sample of navigation actions |
Multi screen navigation | ✅ | MultiScreen, build-in commands | SampleMultiscreen |
Dialogs | ✅ | DialogScreen | Dialogs playground |
Arguments | ✅ | Arguments can be passed as constructor parameter of Screen. | MainScreen |
Animation | ✅ | ScreenTransition, helpers for stack screen transition | SampleStack |
Android lifecycle support | ✅ | ModoScreenAndroidAdapter connect Modo with android-related features. You can safely use LocalLifecycleOwner and other fun inside Screen's. To correctly observe screen lifecycle it's recommender to use LifecycleScreenEffect |
MainScreen |
Android ViewModel support | ✅ | ModoScreenAndroidAdapter connect Modo with android-related features. You can safely use viewModel and other fun inside Screen's. |
AndroidViewModelSampleScreen.kt |
Activity and fragment integration | ✅ | Use rememberRootScreen inside Fragment or Activity . These functions are declared in Modo and automatically handle saving and restoring during Activity lifecycle and process death. |
ModoSampleActivity, ModoFragment.kt, ModoLegacyIntegrationActivity.kt |
Activity and process death handling | ✅ | Automatically provided by Fragment and Activity Modo integration. For more details take a look to |
ModoSampleActivity, ModoFragment.kt |
Screen effects | ✅ | You can use LaunchedScreenEffect and DisposableScreenEffect analogies for Screen , it's linked to life time of a Screen . There is also LifecycleScreenEffect for easy observing lifecycle. See ScreenEffects for details. |
ScreenEffectsSampleScreen.kt |
Pager and LazyList integration | ✅ | You can create custom ContainerScreen and use internal Screen inside Vertical/HorizontalPager and LazyRow/Column . It's vital to define key = {} lambda with InternalContent for correct intagration. For more details take a look to the samples. |
StackInLazyColumnScreen.kt, HorizontalPagerScreen.kt |
You can implement container screen by your own, but Modo provides several default containers
-
StackScreen - stack navigation implementation. Sample of navigation
-
Multiscreen - multiscreen navigation. Sample of navigation
Check out sample for more.
MIT License
Copyright (c) Konstantin Tskhovrebov (@terrakok)
and Karenkov Igor (@KarenkovID)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.