Simplified handling of fragments in android application by using wrapper class around FragmentManager
and simple tag
stack.
Detailed explanation is in this blog post
Gradle
implementation 'com.bajicdusko:fragment-manager:1.0.4'
or
implementation 'com.bajicdusko:kotlin-fragment-manager:1.0.4'
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())
}
}