android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.42k stars 8.29k forks source link

How to observe one livedata on two pages? #1057

Open taknikiniga opened 2 years ago

taknikiniga commented 2 years ago

i'm using navigation component in my android app. i have a search edit text on that i attach a text watcher. when that search edit text come into focus i open a seach fragment but under seach edit text. both pages are in live state .

this code is inside my home viewmodel

 val searchMData: MutableEventLiveData<String> = MutableEventLiveData()
val searchLiveData: LiveData<String>

from home activity i post value inside my ( searchMData ) throughout textwatcher and i want to observe ( searchLiveData ) Inside my Search Fragment.

please help......

vpuonti commented 1 year ago

Please look into using activityViewModels(): https://developer.android.com/guide/fragments/communicate#viewmodel