https://github.com/lk4403/BusUtilsDemo
这个链接里,第一次提交是BusUtils的,插件版本确实是2.6了,第二个提交是EventBus的。
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
EventBus.getDefault().postSticky("Bus Event")
Handler().postDelayed({
startActivity(Intent(this, MainActivity::class.java))
finish()
}, 666)
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
findViewById<View>(R.id.b1).setOnClickListener(listener) // 添加fragment 1
findViewById<View>(R.id.b2).setOnClickListener(listener) // 添加fragment 2
findViewById<View>(R.id.b3).setOnClickListener(listener) // 添加fragment 3
EventBus.getDefault().register(this)
}
override fun onDestroy() {
EventBus.getDefault().unregister(this)
super.onDestroy()
}
@Subscribe(
threadMode = ThreadMode.MAIN,
sticky = true
)
fun onEvent(event: String) {
append(mItemContainer, "$event - ${mItemCount++}")
}
}
class TabFragment : Fragment() {
private lateinit var mItemContainer: LinearLayoutCompat
private var mItemCount = 0
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val tabView = inflater.inflate(R.layout.fragment_tab, container, false)
tabView.findViewById<TextView>(R.id.title).text = arguments!!.getString("title")
mItemContainer = tabView as LinearLayoutCompat
return tabView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
EventBus.getDefault().register(this)
}
override fun onDestroyView() {
EventBus.getDefault().unregister(this)
super.onDestroyView()
}
@Subscribe(
threadMode = ThreadMode.MAIN,
sticky = true
)
fun onEvent(event: String) {
append(mItemContainer, "$event - ${mItemCount++}")
}
}