top of page

Blog

Resources(R.string) in viewModel in Android

I would like to share a technique to access the strings in view model that works well.
So there are multiple ways to access the string in view model but they have there own advantages and disadvantages.
We have options like

  1. Hardcode a string in code but this will not help in case of reusability and string localisation

  2. Use the context of an activity but in mvvm its bad practice to use context direct in view model

  3. Return the string resource id which is integer and in activity get the actual string but this will increase the efforts

​

Link : https://dev.to/sandeepsatpute9271/resourcesrstring-in-viewmodel-in-android-553g

h61huv7qsapq7p9zox7y.webp

​Shared ViewModel: A modern way to share data between android fragments and activities

In android development, developer multiple time come across a situation where needs to share the data between fragments and activities. So there are multiple ways to share the data between fragments and activities such as

  1. Bundle while replacing the fragment/activity

  2. By using interface

  3. Shared viewModel

Let's start with shared viewModel which is the simplest way of sharing the data between fragments/activities.

What is viewModel and responsibility of viewModel
The ViewModel class is designed to store and manage UI-related data in a lifecycle-conscious way. ViewModel class acts as an interface between Fragments to sharing data.
ViewModel Life cycle helps us to hold the data and keep that live even if fragment is replaced.

Shared viewModel
The shared view model is the normal view model only but the data holding behaviour and life cycle of view model help us to make it shared view model.
As per life cycle data is available even if caller fragment get destroy and we can get that data for other fragments.

​

Link : https://dev.to/sandeepsatpute9271/shared-viewmodel-a-modern-way-to-share-data-between-android-fragments-and-activities-31p9

k8yjp8f62d6etpu5oxqx.webp

Kotlin Multiplatform Mobile (KMM)

Basics about the KMM

Kotlin Multiplatform Mobile (KMM) is an SDK that allows us to use the same business logic code in both iOS and Android applications. It offers all the combined benefits of creating cross-platform and native apps. It reduces the time needed for writing and maintaining the same code for different mobile platforms while retaining the flexibility and benefits of native programming. With Kotlin, we can use a single codebase for the business logic of iOS and Android apps and write platform-specific code only where necessary in order to implement a native UI or when working with platform-specific APIs.

Benefits

Share code for logic elements that often fall out of sync while keeping the advantages of native programming, including great app performance and full access to the Android and iOS SDKs.

How KMM works

An app consists of 4 main layers that includes UI logic layer, presentation logic layer, business logic layer, and data Layer. The data layer which has classes with the responsibility of making network calls and database queries can be 100% shared. The presentation and UI logic can be partially shared depending upon the device types and other platform specific peculiarities.

KMM can seamlessly be integrated with the mobile project. Shared code, written in Kotlin, is compiled to JVM bytecode with Kotlin/JVM and to native binaries with Kotlin/Native, so you can use your KMM business-logic modules just like any other regular mobile library.

Kotlin harbours multiplatform libraries that can reuse the multiplatform logic in common and platform-specific code such as HTTP, Serialization etc. To interoperate with platforms, platform-specific versions of Kotlin can be used. Platform-specific versions of Kotlin (Kotlin/JVM, Kotlin/JS, Kotlin/Native) include extensions to the Kotlin language, and platform-specific libraries and tools. Through these platforms one can access the platform native code (JVM, JS, and Native) and leverage all native capabilities.

Case studies

  • Quizlet migrated their business logic from a shared JavaScript approach to KMM and drastically improved the performance of both their Android and iOS applications.

  • Fastwork introduced KMM for the domain and data layers of their application to share business logic and API service between mobile platforms, significantly boosting their team’s productivity.

  • Yandex.Disk started out by experimenting with the integration of a small feature, and when the experiment proved successful, they implemented their whole data synchronization logic in KMM.

  • Also, there are big projects that are using KMM in production ( i.e Netflix, Phillips, and more ) and probably it was not mentioned but Unit Testing is also possible using KMM.

The project structure

As you can see below, the project is divided into three modules:

  • androidApp—contains all the native code for the Android application

  • iosApp—contains all the native code for the iOS application

  • shared—contains all the shared code (eg business logic) to be used in both the Android and iOS platform

Benefits of using KMM

  • Reduced development time and effort

  • Improved code quality

  • No Impact on performance

  • Easy to use

  • UI can be split for each target platform – the app will feel consistent with any given ecosystem

  • Shared logic allows developers to add new features and fix bugs on both operating systems at the same time

  • Easy to integrate: Whether it’s a fresh project or already existing project you can start integrating KMM anytime.

  • Easy to learn: Kotlin syntax is very similar to other popular languages such as Swift and Java. So it’s pretty easy to learn and get started with KMM.

Finish line!

And that's it for now. KMM is a good option if there is something that needs to be shared on different mobile platforms, as new options there are limitations that progressively will be fixed.

Kotlin is an awesome language and Kotlin Multiplatform brings that to other platforms to help us create scalable and maintainable apps using a single codebase. Although the code-sharing is slightly less than that with other cross-platform solutions as UI cannot be shared, that gives us the advantage of creating a user experience that is in line with the platform’s guidelines and best practices.

Basically think about KMM as a new option to share code between platforms that will be improved by the Kotlin community and you have the control of what needs to be shared and what’s not.

Let’s Kotlin!

​

Link : https://dev.to/sandeepsatpute9271/kotlin-multiplatform-mobile-kmm-550j

3tqckyztfn3fqydjxq5k.webp
bottom of page