Architecture-components-samples: How to provide custom scope for ViewModel? (for custom view)

Created on 12 Feb 2018  路  2Comments  路  Source: android/architecture-components-samples

Hi, I have a custom view which gets added and removed in runtime, i.e. its lifecycle could be shorter than the activity. Is there a way provider custom scope for it? Now if VM is scoped to activity, id get the same instance even on a technically another logical instance of the custom view.

Is there a way to provider custom scope? Now I need a "reset" method on the view model which is lame.

Thanks

Most helpful comment

@florina-muntenescu I have same problem, if we cannot use the ACVM in views, so what would be your suggestion as I need to create a complex custom view which needs to interact with data layer, I prefer to keep the logic inside of custom view class to prevent duplication as it will be using on different activities and fragments.

All 2 comments

The Architecture Components ViewModels are made to work with Activities and Fragments, not with the lifecycle of a View.

@florina-muntenescu I have same problem, if we cannot use the ACVM in views, so what would be your suggestion as I need to create a complex custom view which needs to interact with data layer, I prefer to keep the logic inside of custom view class to prevent duplication as it will be using on different activities and fragments.

Was this page helpful?
0 / 5 - 0 ratings