Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Android Jetpack Compose MVVM
Paul Allies
Paul Allies

Posted on

     

Android Jetpack Compose MVVM

Model–View–ViewModel (MVVM) is a software architectural pattern that facilitates the separation of the development of the GUI (the view) from the development of the business logic or back-end logic (the model) so that the view is not dependent on any specific model platform. The ViewModel of MVVM is responsible for exposing the data objects from the model in such a way that objects are easily managed and presented. In this respect, the ViewModel is more model than view and handles most if not all of the view’s display logic. The ViewModel may implement a mediator pattern, organizing access to the back-end logic around the set of use cases supported by the view.

I’ve put together a small sample showing these 3 layers and how they relate to each other in JetPack Compose. You’ll notice that other than property/method names, none of the objects need to know anything about the others. Each layer can be built completely independent of the others.

Sample Model

importjava.util.*data classUser(varid:UUID,varname:String)
Enter fullscreen modeExit fullscreen mode

Sample ViewModel

This should contain everything one would need to interact with the view. Right now it contains 2 members: users, and an addUser function. Note we’re using mutableStateListOf to maintain state of a collection of users in the view model.

importandroidx.compose.runtime.*importandroidx.lifecycle.ViewModelclassUserViewModel:ViewModel(){privateval_users=mutableStateListOf<User>()valusers:List<User>get()=_usersfunaddUser(user:User){_users.add(user)}}
Enter fullscreen modeExit fullscreen mode

Sample View

And now the View. These are DataTemplates that define how data should be displayed. ViewModels survive configuration changes, so they allow you to encapsulate state and events related to the UI without having to deal with the activity or fragment lifecycle that hosts your Compose code.

@ComposablefunUserView(){valvmbyremember{mutableStateOf(UserViewModel())}Scaffold(topBar={TopAppBar(title={Text("Users")},actions={IconButton(onClick={vm.addUser(User(UUID.randomUUID(),"User"))}){Icon(Icons.Filled.Add,null)}})},content={Column(modifier=Modifier.padding(16.dp)){LazyColumn(modifier=Modifier.fillMaxHeight()){items(vm.users){Column{Text(it.name)Text("${it.id}")}}}}})}
Enter fullscreen modeExit fullscreen mode

We can hoist the ViewModel further up the Compose UI tree to maintain a more global state as follows:

classMainActivity:ComponentActivity(){overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)valvm=UserViewModel()setContent{MaterialTheme{UserView(vm)}}}}@ComposablefunUserView(vm:UserViewModel){Scaffold(topBar={TopAppBar(title={Text("Users")},actions={IconButton(onClick={vm.addUser(User(UUID.randomUUID(),"User"))}){Icon(Icons.Filled.Add,null)}})},content={Column(modifier=Modifier.padding(16.dp)){LazyColumn(modifier=Modifier.fillMaxHeight()){items(vm.users){Column{Text(it.name)Text("${it.id}")}}}}})}
Enter fullscreen modeExit fullscreen mode

Image description

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Founder and Software Developer at Nanosoft (PTY) Ltd
  • Location
    Cape Town, South Africa
  • Education
    BSc. Elec Eng (U.C.T)
  • Work
    Founder at Nanosoft
  • Joined

More fromPaul Allies

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp