Farhana Naaz Ansari
Farhana Naaz Ansari

Reputation: 7944

how to call interface method in view model of fragment class in kotlin

I have an Activity which is called Homeactivity and I have a fragment Notification and their ViewModel class, I want to update notification count from fragment's ViewModel and I declared that interface outside the notification ViewModel and I'm unable to call the method of that interface in Kotlin

class NotificationsViewModel(val notificationsActivity: HomeActivity) : 
          BaseObservable() {  


 //somewhere i want to update count which is in homeactivity

    } 
  interface NotifyCount {
    fun notifyNotificationCount(count: String)

}

My fragment class is given below

class NotificationsFragment : Fragment() {
private var notificationsBinding: NotificationsActivityBinding? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    notificationsBinding=  DataBindingUtil.inflate(inflater, R.layout.notifications_activity,container,false)
    notificationsBinding!!.viewModel= NotificationsViewModel(activity!! as HomeActivity)
    return notificationsBinding!!.root
}

}

I just want to call method notifyNotificationCount in ViewModel so that I will update my count from HomeActivity by overriding the NotifyCount

how can I call the method notifyNotificationCount in viewModel` class?

Upvotes: 1

Views: 1960

Answers (1)

Virendra Varma
Virendra Varma

Reputation: 935

You can use MutableLiveData in your ViewModel class and observe the value of notification count, whenever the value of notification count changed it will inform to observing the class.

Upvotes: 1

Related Questions