# Encapsulating View State

DevFeed: [Encapsulating View State](<https://devfeed.tech/articles/encapsulating-view-state-30593.md>)

Original publisher: [Read original article](<https://ryanharter.com/blog/2019/07/encapsulating-view-state/>)

Published: 2019-07-19T16:00:00Z

Content type: tutorial

Language: en

Sources: [Blogs on Ryan Harter](<https://devfeed.tech/sources/blogs-on-ryan-harter.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [business logic](<https://devfeed.tech/topics/business-logic.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [App](<https://devfeed.tech/topics/app.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [app](<https://devfeed.tech/tags/app.md>), [app-architecture](<https://devfeed.tech/tags/app-architecture.md>), [business-logic](<https://devfeed.tech/tags/business-logic.md>), [code](<https://devfeed.tech/tags/code.md>), [error](<https://devfeed.tech/tags/error.md>), [livedata](<https://devfeed.tech/tags/livedata.md>), [messages](<https://devfeed.tech/tags/messages.md>), [testing](<https://devfeed.tech/tags/testing.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

## AI overview

This article presents an approach to encapsulating view state in Android applications with AndroidX ViewModel. It explains how putting business logic and decision-making code in ViewModels can simplify Activities and Fragments and make the valuable code easier to test.

## Source excerpt

The AndroidX ViewModel has become a central component in many modern Android apps. They offer a relatively simple abstraction to encapsulate business logic and decision making code which separates it from the platform frameworks, which are often difficult to test. Since ViewModels are easier to test than Framework classes like Activity and Fragment due to their simple lifecycles and lack of external dependencies, it makes sense to try to put as much logic, the complex decision-making code that should be tested, in ViewModels as possible. Ideally we'll be left with a view layer, either Activities, Fragments, or something else, which is so simple that it's not worth testing.