Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Android View State Machine Part 1

 

 
Egbai Smile Mmumene
Technology lover, mobile developer, travel enthusiast
Published: August 12, 2020
Tweet
Share
 

 

In software development one of the most prevalent things is state management or simply put, knowing what to do and when to do it. The main focus of this series would be the presentational layer of an app and devise modelling of received data into a state object and interpretation at the view i.e managing the view in correspondence to emitted state.

Every user interaction event triggers a process, this could be an HTTP request, a CRUD operation, a scroll action, or simply launching a new activity. It’s very important to note that each process can lead to a new state.

 

A state is the set of values of all variable aspects of the particular system under concern at an instant in time. i.e the remembered information is called the state of the system. “Stephen Kurtzman PhD”

A finite state machine is an abstract machine that can be in exactly one of a finite number of states at any given time. “Wikipedia”

 

It’s safe to say, that when building screens in our apps, whether we choose to define finite states for these screens or not they already exist and how we access them or group such state-based data is important to both the sanity of the app and that of other developers involved, as well as guarding against irregular transitions or leaks in state.

 

Image for post

a typical state transition from app start to loaded screen or error screen

 

The above diagram shows a typical applications state lifecycle or as we like to call it a “flow”. Each state represents either the presence of a certain type of data or the absence of it, and we would have associative views to show the user the current state of the app, which could be a progress bar for the loading state, an error message/screen for error state or a scrollable list for depicting a successful data load. Representing this data in our code in concise and clear fashion is important as it helps us to successfully separate and group operations particular to each state.

 

 

The snippet above represents a general state overview of what our diagram tries to represent. Defining a class to represent finite states in our Views is important because this helps us group operations to perform on emittanceof a particular object ViewState, rather than checking across wide form factors of data variables and have a “guess” as to what state the application is in.

After creating our Abstract state model, in our fragments or activities, we would have a resolver (usually a when or a switch case statement) that would match functions or grouped procedures to each emitted state. In other instances the states would be mapped to a Viewholder or widget and we would emit a StatefulViewObject and when resolving in the View we would just inflate the view object, we would take a look at this in the next part of this series.

 

Image for post

 

Let’s see how we would put this above illustration into practice.

 

 

In the above sample, we have a method that loads a list of data characters the CharacterToUIMapper() is responsible for mapping the received object into the supposed ViewState for emission to the view. The function CharacterToUIMapper() would check the response gotten from the loadCharactersUseCase() and then push the relevant ViewState object into the liveData .

 

 

Subsequently after emitting our ViewState object, we would receive it in the view, and depending on the object the appropriate function would then populate the associated view

 

Finally…

 

Managing states and view hierarchies are one of the things we find ourselves consistently doing in android app development, it’s better we define bounds and set rules to our views and what state would be associated with them. This not only makes debugging problems easier but also makes adding extra view logic/state a run in the park. Whether we choose to explicitly define our states, it doesn’t stop the states from being present.

 

 

View original article at: 


 

Originally published: July 29, 2020

Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03

By Carlos Mota

It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update. 

By ProAndroidDev -
Android News
Noisy Code 🗣 With Kotlin Scopes
Noisy Code 🗣 With Kotlin Scopes

By Chetan Gupta

Scopes make your code more readable? think again... You are going to encounter these scope functions namely let, run, apply, also, within every Kotlin codebase, along with all the mischievous ways developers exploit their usage from the way they were intended for. Let see how popular opinion on those ends up just as a code noise.

By ProAndroidDev -
Android News
Improving Android DataBinding with Bindables library
Improving Android DataBinding with Bindables library

By Jaewoong Eum

DataBinding is one of the most important factors for MVVM architecture. The basic concept of DataBinding is to link the view and view model via observer patterns, properties, event callbacks, etc. Linking and automating communication between the view via the bound properties or something in the view model has a lot of benefits in the MVVM architecture concept.

By ProAndroidDev -
Android News
KMM QuickStart Guide
KMM QuickStart Guide

By Mayank Kharbanda

Kotlin Multiplatform (KMP) is a code-sharing technology that allows us to use the same code for different platforms whether it’s JVM, Android, iOS, watchOS, tvOS, Web, Desktop, or WebAssembly. In this article, we will use Kotlin Multiplatform Mobile (KMM) which is a subset of KMP with the focus on providing better tooling and support for sharing code on mobile platforms i.e. Android and iOS.

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One