Skip to content

Simplified handling of fragment operations in android app.

License

Notifications You must be signed in to change notification settings

bajicdusko/SimpleFragmentManager

Repository files navigation

SimpleFragmentManager

Simplified handling of fragments in android application by using wrapper class around FragmentManager and simple tag stack.

Detailed explanation is in this blog post

SimpleFragmentManager SimpleFragmentManager

Installation

Gradle

implementation 'com.bajicdusko:fragment-manager:1.0.4'

or

implementation 'com.bajicdusko:kotlin-fragment-manager:1.0.4'

Usage example

To simplify the setup, few super classes are created: SFMActivity, SFMFragment and FragmentChannel. By extending these three classes you should be ready in no time.

Create new communication interface by extending FragmentChannel

interface ExampleFragmentChannel : FragmentChannel {
    fun openSecondFragment()
}

Each new fragment handled by SimpleFragmentManager should extend SFMFragment<>

class FirstFragment : SFMFragment<ExampleFragmentChannel>() { 

    ...
    
    fun onButtonClick(){
        fragmentChannel?.openSecondFragment()
    }
}

By creating new activity, just extend SFMActivity

class HomeActivity : SFMActivity(), ExampleFragmentChannel {

    override fun getFrameLayoutContainerId(): Int = R.id.activity_home_fragment_container

    override fun onCreate(savedInstanceState: android.os.Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        simpleFragmentManager.enableLogs(true)
        simpleFragmentManager.addFragment(FirstFragment.newInstance())
    }
    
    override fun openSecondFragment(){
        simpleFragmentManager.replaceFragment(SecondFragment.newInstance())
    }
}

This is what happens in the background

SequenceDiagram