Essenty
The most essential libraries for Kotlin Multiplatform development.
Supported targets:
android
jvm
js
(IR
andLEGACY
)iosArm64
,iosX64
watchosArm32
,watchosArm64
,watchosX64
tvosArm64
,tvosX64
macosX64
linuxX64
Lifecyle
When writing Kotlin Multiplatform (common) code we often need to handle lifecycle events of a screen. For example, to stop background operations when the screen is destroyed, or to reload some data when the screen is activated. Essenty provides the Lifecycle
API to help with lifecycle handling in the common code. It is very similar to Android Activity lifecycle.
Setup
Groovy:
Kotlin:
Lifecycle state transitions

Content
The main Lifecycle interface provides ability to observe the lifecycle state changes.
The LifecycleRegistry interface extends both the Lifecycle
and the Lifecycle.Callbacks
at the same time. It can be used to manually control the lifecycle, for example in tests.
The LifecycleOwner just holds the Lifecyle
. It may be implemented by an arbitrary class, to provide convenient API.
Android extensions
From Android, the Lifecycle
can be obtained by using special functions, can be found here.
Usage example
The lifecycle can be observed using its subscribe
/unsubscribe
methods:
Or using the extension functions:
Parcelable and Parcelize
Essenty brings both Android Parcelable interface and the @Parcelize
annotation from kotlin-parcelize compiler plugin to Kotlin Multiplatform, so they both can be used in common code. This is typically used for state/data preservation over Android configuration changes, when writing common code targeting Android.
Setup
Groovy:
Kotlin:
Usage example
Once the dependency is added and the plugin is applied, we can use it as follows:
When compiled for Android, the Parcelable
implementation will be generated automatically. When compiled for other targets, it will be just a regular class without any extra generated code.
Parcelize for Darwin/Apple targets
Currently there is no extra code generated when compiled for Darwin/Apple targets. However I made a proof of concept: kotlin-parcelize-darwin compiler plugin. It is not used yet by Essenty, and the applicabilty is being considered. Please raise a Discussion if you are interested.
StateKeeper
When writing common code targetting Android, it might be required to preserve some data over Android configuration changes or process death. For this purpose, Essenty provides the StateKeeper
API, which is inspired by the AndroidX SavedStateHandle. The StateKeeper
API relies on the Parcelable
interface provided by the parcelable
module described above.
Setup
Groovy:
Kotlin:
Content
The main StateKeeper interface provides ability to register/unregister state suppliers, and also to consume any previously saved state.
The StateKeeperDispatcher interface extens StateKeeper
and allows state saving, by calling all registered state providers.
The StateKeeperOwner interface is just a holder of StateKeeper
. It may be implemented by an arbitrary class, to provide convenient API.
Android extensions
From Android side, StateKeeper
can be obtained by using special functions, can be found here.
Usage example
InstanceKeeper
When writing common code targetting Android, it might be required to retain objects over Android configuration changes. This use case is covered by the InstanceKeeper
API, which is similar to the AndroidX ViewModel.
Setup
Groovy:
Kotlin:
Content
The main InstanceKeeper interface is responsible for storing object instances, represented by the [InstanceKeeper.Instance] interface. Instances of the InstanceKeeper.Instance
interface survive Android Configuration changes, the InstanceKeeper.Instance.onDestroy()
method is called when InstanceKeeper
goes out of scope (e.g. the screen is finished).
The InstanceKeeperDispatcher interface extens InstanceKeeper
and adds ability to destroy all registered instances.
The InstanceKeeperOwner interface is just a holder of InstanceKeeper
. It may be implemented by an arbitrary class, to provide convenient API.
Android extensions
From Android side, InstanceKeeper
can be obtained by using special functions, can be found here.
Usage example
BackPressedDispatcher
The BackPressedDispatcher
API provides ability to handle back button clicks (e.g. an Android device's back button), in common code. This API is similar to AndroidX OnBackPressedDispatcher.
Setup
Groovy:
Kotlin:
Content
The BackPressedHandler interface provides ability to register/unregister back button handlers. When the device's back button is clicked, all registered handlers are called in reverse order. If a handler returns true
then the event is considered as handled and the handling process stops, the remaining handlers are not called. If none of the handlers returned true
then the event is considered as unhandled.
The BackPressedDispatcher interface extends BackPressedHandler
and is responsible for triggering the registered handlers. The BackPressedDispatcher.onBackPressed()
triggers all registered handlers in reverse order, returns true
if the event is handled, and false
if the event is unhandled.
Android extensions
From Android side, BackPressedDispatcher
can be obtained by using special functions, can be found here.
⚠️ Due to the nature of AndroidX
OnBackPressedDispatcher
API, it is not possible to map it 1-1 toBackPressedHandler
. Please keep in mind some possible side effects described in the corresponding KDocs.
Usage example
Author
Twitter: @arkann1985