Alon Shlider
Alon Shlider

Reputation: 1298

Shared livedata observer not working between Fragment and Activity

I have a viewmodel that is instantiated via Koin library at my MainActivity -

class SharedInformationViewModel : ViewModel() {
    val deviceId : MutableLiveData<String> = MutableLiveData<String>()
}

class MainActivity : AppCompatActivity() {

    private val sharedInformationViewModel: SharedInformationViewModel by inject()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        setContentView(binding.root)

        sharedInformationViewModel.deviceId.observe(this, { deviceId ->
            //This is not called no matter what I do!
            val x = 5
        })
    }

And I changed the value of 'deviceId' in another Fragment -

class SettingsFragment : Fragment() {

    private val sharedInformationViewModel: SharedInformationViewModel by inject()


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

       
       //Alot of code here ...
       if (deviceId != activity?.sharedPreferences?.deviceId) {
            sharedInformationViewModel.deviceId.value = deviceId
            activity?.sharedPreferences?.deviceId = deviceId
            wasSettingsDataChanged = true
        }
    }

The value indeed is getting changed but the observer is never called.

I can't figure out what's wrong and what is it that I am missing.

Tried to use Koin's 'get<>()' method instead of 'by inject()' but did not help.

Upvotes: 1

Views: 1884

Answers (2)

Baki Kocak
Baki Kocak

Reputation: 389

According to official documentation, viewModel should be initialize as:

 //Use the 'by activityViewModels()' Kotlin property delegate
 // from the fragment-ktx artifact

 private val model: SharedViewModel by activityViewModels()

Upvotes: 1

EpicPandaForce
EpicPandaForce

Reputation: 81529

You need to use

// Activity
private val sharedInformationViewModel: SharedInformationViewModel by viewModel()

// Fragment
private val sharedInformationViewModel: SharedInformationViewModel by sharedViewModel()

And

= module {
    viewModel {
        SharedInformationViewModel()
    }
}

Upvotes: 3

Related Questions