Instantly share code, notes, and snippets.

View FragmentOne.kt
class FragmentOne : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_fragment_one, container, false).apply {
this.go_to_fragment_two.setOnClickListener {
RxBus.instance.publish(FRAGMENT_TWO_TAG)
}
View FragmentTwo.kt
class FragmentTwo : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_fragment_two, container, false).apply {
this.back_to_fragment_one.setOnClickListener {
RxBus.instance.publish(FRAGMENT_ONE_TAG)
}
View MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
observeEvents()
RxBus.instance.publish(FRAGMENT_ONE_TAG)
}
private fun observeEvents() {
View fragment_two.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.FragmentTwo">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_fragment_two"
View fragment_one.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.FragmentOne">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_fragment_one"
View activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
View RxBus.kt
class RxBus private constructor() {
private val subject = PublishSubject.create<Any>()
fun publish(message: Any) {
subject.onNext(message)
}
fun subscribe(action: (Any) -> Unit): Disposable {
return subject.subscribe { action(it) }
View AwesomeActivity.kt
class AwesomeActivity : Activity(), CallbackListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstance)
setContentView(R.layout.awesome_activity)
}
override fun onCallback() {
Toast.makeText(context, "onCallback called!", Toast.LENGTH_LONG).show()
}
}
View AwesomeFragment.kt
class AwesomeFragment : Fragment() {
private var callback: CallbackListener? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_awesome, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
View CallbackListener.kt
interface CallbackListener {
fun onCallback()
}