I have a problem when using ViewModel
and LiveData
arch components. When using fragments
and rotating the screen, the observer gets triggered...
I tried to move viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
in all the fragment lifecycle methods, but with no success.
My scenario is relatively straightforward:
- Login screen with email and password
- User clicks on the "login" button
- The viewmodel calls the
login(email, password)
and sets the value of the LiveData object - Just for now simply show a Toast
At this point everything is okay. But when I rotate the screen the Toast appears again without any user interaction.
Do I have to do something in onDestroyView()
?
Thanks in advance!