override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater.inflate(R.layout.fragment_demo, container, false) binding = FragmentDemoBinding.bind(view).apply { viewModel = (activity as DemoActivity).obtainViewModel().also { subscribe(it) } } binding.setLifecycleOwner(viewLifecycleOwner) return view } private fun subscribe(viewModel: DemoViewModel) { lifecycle.addObserver(viewModel) viewModel.message.observe(this, Observer { showToast(it) }) } companion object { fun newInstance(): DemoFragment = DemoFragment() } } class DemoFragment : BaseFragment() { private lateinit var binding: FragmentDemoBinding override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater.inflate(R.layout.fragment_demo, container, false) binding = FragmentDemoBinding.bind(view).apply { viewModel = (activity as DemoActivity).obtainViewModel().also { subscribe(it) } } binding.setLifecycleOwner(viewLifecycleOwner) return view } private fun subscribe(viewModel: DemoViewModel) { lifecycle.addObserver(viewModel) viewModel.message.observe(viewLifecyclerOwner, Observer { showToast(it) }) } companion object { fun newInstance(): DemoFragment = DemoFragment() } }