Search code examples
androidandroid-architecture-componentsandroid-livedata

How it impact on memory if I create all View Models with Activity scope?


I'm using new Android architectural components : MVVM, LiveData, etc. As recommended by Android documentation as well as in many blogs, in order to share data between fragments, we should create SharedViewModel which will be accessed by each fragment using ViewModelProviders with Activity scope.

I just want to know, Is there any impact on memory, if I create all my ViewModels with Activity scope? Do they automatically destroyed on Android low memory?


Solution

  • No you should aware about memory issue.

    In the navigation AAC, Google recommands single activity application.

    But that is hard in some part.

    So I use separated activity to using same view model scope.

    If a, b, c must use same Viewmodel, just make a A Activity for them and separate with d, e fragment(in B Activity).

    Activity and Viewmodel lifecycle