Skip to content

Use ViewModels #5070

@westnordost

Description

@westnordost

So far, the data in the view layer has been kept in the Fragments and Activitys. Since these can be destroyed and recreated when the Android system decides to do this, the serialization and deserialization into a savedInstanceState must be done manually. Moving to ViewModels has the following advantages:

  1. It's less error prone, possibly also less code. The data is not bound to the view lifecycle and hence does not have to be manually re-created

  2. It provides a better separation between view logic and view data. Thus, the latter is likely mostly platform independent and could be re-used on other platforms with less effort. (View model patterns exists for iOS too, there is at least one kotlin multiplatform library in development that one might switch to in the future).

  3. It will likely simplify some of the "talks to parent via listener" pattern, because some, or most of it(?) can probably be done with shared view models instead (i.e. a view model that is shared between a fragment and its parent fragment / activity).

  4. It's what the cool kids use! Joking. It is actually pretty standard to use since many years and the more standard things are used in StreetComplete, the easier it is for new contributors to join

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Released

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions