How Can We Achieve Shared View Model Communication Between A Fragment And Activity, Where The Activity Is Not The Parent
I am trying to achieve Fragment to Activity communication given that the Activity is not the parent Activity. So, I have a MainActivity that has a fragment called ContactListFragme
Solution 1:
Write an interface class with object/objects/Data types you need to sync
interfaceOnSaveClickListener{
funonSaveClicked(contact: Contact)
}
Now in ContactListFragment class
classContactListFragment : Fragment(), OnSaveClickListener {
overridefunonCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(activity as AddContactACtivity).mOnSaveClickListener = this
}
overridefunonSaveClicked(contact: Contact) {
// Whatever you want to do with the data
}
}
In AddContactActivity,
classAddContactActivity {
var mOnSaveClickListener : OnSaveClickListener? = nullprivatevoidwhenYouClickSave(contact: Contact){
mOnSaveClickListener?.onSaveClicked(contact)
}
Post a Comment for "How Can We Achieve Shared View Model Communication Between A Fragment And Activity, Where The Activity Is Not The Parent"