Image for post
Image for post
Photo by Pablo García Saldaña on Unsplash

Introduction

With the rise of KMM (Kotlin multiplatform mobile) hype, how about cleaning up as much Android dependencies as we can to increase the platform-independent code?
LiveData is widely used by Android community to manage and represent screen states. In order to safely replace it, we need to consider some implementation details.

Developing for Android is complex for several reasons, and one of them is working with the lifecycle of its main components: Activity and Fragment. Not handling the lifecycle properly may lead to memory leaks, crashes and unexpected states.


Image for post
Image for post
Photo by Mihai Moisa on Unsplash

After the announcement of the StateFlow implementation this year, I got curious about the possibility to totally replace LiveData. This means one less project dependency and achieve a more independent code from Android framework.

StateFlow is not the subject of this post but we can change the view states represented by a LiveData using a StateFlow. For SingleLiveEvent class, we need a different solution.

Following the MVVM pattern, ViewModel provides the view state and events/actions to the View. In the context of LiveData, the second could be implemented using the class SingleLiveEvent. …


Image for post
Image for post

TL;DR
Use ItemDecoration instead of setting a margin inside the item.xml.

List is a common component that every Android developer implements in its routine/daily basis. At some point, we need to add a divider between items and/or just adjust the margin for cards to be displayed harmonically. As it’s a common problem, there are plenty of old answers on the internet on how to solve them but most of them are not the best solution. Let’s stick with the margin issue.

Problem

Suppose we’re creating a screen that displays a list of cards that each is expected to have a margin of 16dp. We build the item.xml

About

Cesar Morigaki

Android Developer @iFood

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store