0

Before I post detailed code, I want to make sure I do understand LiveData correctly. I have a MainActivity (A) and then go to Activity (B). I did implement a ViewModel for 'B' and observe (and display) the MutableLiveData for a Counter (residing in the ViewModel) in Activity 'B'.

This works perfect, I do see the countdown in 'B', but when I do the back-button to return to MainActivity 'A' and then go back to 'B', the counter is not updated anymore.

I do see the counter pick up nicely when I switch apps ie send it to the background and bring it back.

I might not understand the whole Lifecycle properly, but I thought the beauty of LiveData is that I don't have to worry about all this and once I return to 'B' it should just pick up the observation seamlessly?

PS> Forgot to mention that I do see the counter counting down uninterrupted while I'm in 'A' (via LOG.i)

Mairyu
  • 809
  • 7
  • 24
  • I think when you are going from A->B, activity B gets re created and so it is not showing the counter updated. Trying setting the launch mode in manifest to single top. Imay be wrong but I think this will solve your problem. – primo Sep 20 '22 at 04:16
  • I added android:launchMode="singleTop" to activity 'B', but still see the same behavior. So is my basic understanding correct and LiveData/ViewModel is the correct approach to be able to go back and forth between activities while being able to observe the same counter in multiple activities (or in the same 'B' after I return?) – Mairyu Sep 20 '22 at 04:31

1 Answers1

1

When you go from A > B -- A is alive B is alive Back button to go to A -> A is alive B is destroyed hence the ViewModel associated with B is destroyed as well. Button to go from A > B -> A is alive and a new instance of ViewModel B is created. This new ViewModel of B has no idea the previous ViewModel existed.

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
  • That's what I thought, but if, as you say, ViewModel 'B' is destroyed, why does the 'Log.i' still count down the counter? This seems to indicate to me that ViewModel 'B' is doing just fine. Maybe a 2nd (newer) ViewModel 'B' is created and Activity 'B' is connecting to the 2nd (new) ViewModel instead? In that case, is there a way to achieve what I tried to lay out in my original question? ie having a single counter that I can display any time I enter activity 'B'? I hope this makes sense ... – Mairyu Sep 20 '22 at 04:40
  • yes you need to have a shared Viewmodel. A ViewModel whose lifecycle owner is Activity A .. that way this Viewmodel will be persisted as long as A is persisted. A easier solution IMO is just saving it in shared pref and getting value from there, – Narendra_Nath Sep 20 '22 at 04:42
  • What's your definition of a 'shared' viewmodel? Digging around more, there doesn't seem to be a way to truly share a single viewmodel between multiple Activities: https://stackoverflow.com/questions/62845238/how-can-i-share-viewmodel-between-activities But I think you can share it between fragments of the same Activity, so I might try to merge my Activity 'B' into Activity 'A' with two fragments and try to share the ViewModel there. (not sure a counter is a good example to use in a 'shared pref'?) – Mairyu Sep 20 '22 at 21:44
  • Yeah i went back and checked.. This might help . https://stackoverflow.com/a/57674983/13533028 – Narendra_Nath Sep 21 '22 at 04:28