Skip to content

Instantly share code, notes, and snippets.

@mobibob
Created January 14, 2021 19:21
Show Gist options
  • Save mobibob/d74d2e1ad9a08986dfb885371e27d096 to your computer and use it in GitHub Desktop.
Save mobibob/d74d2e1ad9a08986dfb885371e27d096 to your computer and use it in GitHub Desktop.
kotlin deprecated ViewModelProviders (alternative)
class DashboardFragment : Fragment() {
private lateinit var dashboardViewModel: DashboardViewModel
private var btnPodcast: Button? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// NOTE: The template Activity uses the deprecated ViewModelProviders constructor.
dashboardViewModel = ViewModelProvider.AndroidViewModelFactory(Application()).create(DashboardViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
val textView: TextView = root.findViewById(R.id.text_dashboard)
dashboardViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = it
})
return root
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment