Skip to content

Basic Usage

erkutaras edited this page Nov 24, 2019 · 3 revisions

Basic Usage

Create your root fragments with a provider as List<() -> Fragment>

private val rootFragmentProvider: List<() -> Fragment> = 
    listOf(
        { homeFragment },
        { searchFragment },
        { settingsFragment }
    )

Initialize navigator in your activity

val navigator: MultipleStackNavigator = 
    MultipleStackNavigator(
        fragmentManager, 
        R.id.fragmentContainer,
        rootFragmentProvider,
        navigatorListener
    )

Call initialize in onCreate

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    navigator.initialize(savedInstanceState)
        
    // ...
}

Call onSaveInstanceState in onSaveInstanceState

override fun onSaveInstanceState(outState: Bundle) {
    navigator.onSaveInstanceState(outState)
    super.onSaveInstanceState(outState)
}

Listen for changes to update UI

private val navigatorListener = object : NavigatorListener {
    override fun onTabChanged(tabIndex: Int) {
        //Update your UI. Medusa will notify you when tab is changed.
    }
}

Override onBackPressed()

override fun onBackPressed() {
    if (navigator.canGoBack()) {
        navigator.goBack()
    } else {
        super.onBackPressed()
    }
}

Start fragment

navigator.start(newFragment) 

You can find samples for Kotlin and Java.

Clone this wiki locally