Skip to content

Instantly share code, notes, and snippets.

@wellingtoncosta
Created January 24, 2019 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wellingtoncosta/e78ddbd923cca99bb6b83a10f19f81fd to your computer and use it in GitHub Desktop.
Save wellingtoncosta/e78ddbd923cca99bb6b83a10f19f81fd to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
observeEvents()
RxBus.instance.publish(MAIN_ACTIVITY_SUBJECT, FRAGMENT_ONE_TAG)
}
private fun observeEvents() {
RxBus.instance.subscribe(MAIN_ACTIVITY_SUBJECT, this) {
when (it as String) {
FRAGMENT_ONE_TAG -> {
supportFragmentManager.transaction {
replace(R.id.frame_layout, FragmentOne(), FRAGMENT_ONE_TAG)
}
}
FRAGMENT_TWO_TAG -> {
supportFragmentManager.transaction {
replace(R.id.frame_layout, FragmentTwo(), FRAGMENT_TWO_TAG)
}
}
FINISH_APP_TAG -> this.finish()
}
}
}
override fun onDestroy() {
super.onDestroy()
RxBus.instance.unregister(this)
}
companion object {
const val MAIN_ACTIVITY_SUBJECT = "MAIN_ACTIVITY_SUBJECT"
const val FRAGMENT_ONE_TAG = "FRAGMENT_ONE"
const val FRAGMENT_TWO_TAG = "FRAGMENT_TWO"
const val FINISH_APP_TAG = "FINISH_APP"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment