Kotlin is a statically-typed programming language that runs on the JVM. It has been used in production apps by companies like Pinterest, Spotify, and Twitter since 2016.
The skydoves is a syntactic sugar of Kotlin that allows instantiating Android lifecycle-aware properties.
For initializing Android lifecycle-aware properties, Kotlin provides a lazy and fluent syntactic sugar.
Ah… I’m a complete slacker. I’d want to specify both initialization and disposition at the same time.
Incorporated into your project
Gradle
Add the following code to your build.gradle file (not your module build.gradle file).
mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() ma
Also, in your module’s build.gradle file, add a dependency code.
implementation “com.github.skydoves:lazybones:1.0.2” dependencies
SNAPSHOT
Lazybones snapshots of the current development version are available, which keep track of the most recent versions.
maven url ‘https://oss.sonatype.org/content/repositories/snapshots/’ repositories
Usage
lifecycleAware
The lifecycleAware keyword may be used to lazy initialize a lifecycle-aware object. As the lifecycleOwner’s lifecycle changes, the lifecycleAware capability may be used to register and unregister listeners, clear something, display and dismiss, and dispose of disposable items (Activity, Fragment). We should utilize the by keyword and the lazy() function to lazy initialize an object.
getDarkThemeDialog(baseContext) by lifecycleAware. var myDialog: Dialog by lifecycleAware. on When the lifecycle’s state is on, use this.show() to display the dialog. .onDestroy this.dismiss() / when the lifecycle’s state is on, dismiss the dialog. .lazy() / start the dialog in a lazy manner.
We may skip the this keyword in the onCreate and onDestroy lambda functions. The MediaPlayer will be initialized in the following example, and start() will be called on the lifecycle’s onCreate state. And, depending on the stage of the lifecycle, the pause(), stop(), or release() functions will be called.
a private room mediaPlayer: lifecycleAware’s MediaPlayer MediaPlayer.create(this, R.raw.bgm3) is a method for creating a media player. isLooping = true start() onCreate onStop pause() pause() pause() pause() pause() pause() pause() pause() pause start() when onResume is called. stop() release() onDestroy. lazy()
The above code functions in the same way as the following codes.
a private room mediaPlayer: lazy’s MediaPlayer create(this, R.raw.bgm3) MediaPlayer.create(this, R.raw.bgm3) onCreate(savedInstanceState: Bundle?) override fun mediaPlayer.isLooping = true super.onCreate(savedInstanceState) mediaPlayer.start() is a method for starting a media player. onPause override fun () mediaPlayer.pause() super.onPause() super.onStop() mediaPlayer.pause() override fun onStop() onResume override fun () mediaPlayer.start() super.onResume() super.onDestroy() mediaPlayer.stop() mediaPlayer.release() override fun onDestroy()
RxJava2’s CompositeDisposable
In RxJava2, here’s an example of CompositeDisposable. When the CompositeDisposable is slowly initialized, the dispose() function is automatically executed when onDestroy is called.
val compositeDisposable according to the lifecycle CompositeDisposable() is aware. .lazy() / initialize a CompositeDisposable lazily when onDestroy this activity. onDestroy dispose() / invoke the dispose() function when onDestroy this activity.
Methods pertaining to the lifecycle
As the lifecycle changes, we may call lambda functions, and lifecycleAware has eight lifecycle-related methods.
.onCreate / the lambda will be called when onCreate is called..onStart / the lambda will be called when onStart is called..onResume / the lambda will be called when onResume is called..onPause / the lambda will be called when onPause is called. .onStop / the lambda will be called when the lifecycle state is changed..onDestroy / the lambda will be called when the lifecycle state is changed..onAny / the lambda will be called anytime the lifecycle state is changed..on(On.Create) / we may set the lifecycle state manually as an attribute.
In the non-lifecycle owner class, there are a variety of uses.
The lifetime of a product Aware is a lifecycleOwner extension that may be used on classes that aren’t lifecycleOwners.
class MainViewModel(lifecycle ViewModel() : private val compositeDisposable by lifecycle (Owner: LifecycleOwner) : ViewModel() : ViewModel() : ViewModel() : ViewModel() : ViewModel() : ViewModel() : Owner.lifecycle CompositeDisposable() is aware. it.dispose() is called onDestroy. laziness()…
LifecycleAwareProperty
Here’s a more straightforward solution if we don’t need to initialize lazily. The lifecycleAware keyword may be used to define a LifecycleAwareProperty. The value of the attribute will not be initialized in a lazy manner. As a result, we don’t have to utilize it with the by keyword or the lazy() function.
lifetime of a private variable Lifecycle = AwareProperty When onDestroy, Aware(CompositeDisposable()) / watch the status of the lifecycle and invoke the dispose() method. disposal() observeOnDestroy
We may also use the value field to go to the original property.
lifecycleAwareProperty.value.add(disposable) lifecycleAwareProperty.value.dispose()
The observe_ method may be used to track changes in the lifecycle.
class AppCompatActivity() is the main activity; private val lifecycle is the lifecycle of the app. Lifecycle = AwareProperty DialogueUtil.getDarkTheme() is aware. showOnCreate observeOnCreate dismiss() if observeOnDestroy is true. .. observeOnAny.. … observeOn(On.CREATE)…
Here’s how to do that in Kotlin DSL.
lifetime of a private variable Lifecycle = AwareProperty getDarkThemeDialog() is aware. onCreate(), onResume(), onDestroy(), onDestroy(), onDestroy(), onDestroy(), onDestroy(), onDestroy(), onDestroy(), onDestroy(), onDestroy(), onDestroy(), onDestroy()
Using in the owner class that isn’t a lifecycle owner
The lifetime of a product Aware is a lifecycleOwner extension that may be used on classes that aren’t lifecycleOwners.
class MainViewModel(lifecycle ViewModel (Owner: LifecycleOwner) () private valise TAG = MainViewModel::class.java.simple TAG = MainViewModel::class.java.simple TAG = MainViewModel: Name a private room lifecycleAwareProperty = lifecycleAwareProperty = lifecycleAwareProperty = lifecycle Owner.lifecycle Aware(Rabbit()) this.lifecycleAwareProperty is initialized. log.d(TAG, “OnCreate: $this”) observeOnCreate log.d(TAG, “OnStart: $this”) observeOnStart log.d(TAG, “OnResume: $this”) observeOnResume log.d(TAG, “OnPause: $this”) observeOnPause log.d(TAG, “OnStop: $this”) observeOnStop log.d(TAG, “OnDestroy: $this”) observeOnDestroy observeOnAny is a command that allows you to keep an eye on any object. observeOn(On.CREATE) / observeOn(On.CREATE) / observeOn(On.CREATE) / observeOn(On …
Flow and Coroutines
In the build.gradle file for your module, add a dependency code.
over the 2.4.0-alpha01 requirements implementation “androidx.lifecycle:lifecycle-runtime-ktx:$versions.lifecycle”
For launching suspend functions, we may use the coroutines lifecycleScope. For the lifecycleScope, the Lifecycle-runtime-ktx provides launchWhenStarted, launchWhenCreated, and launchWhenResumed. Those coroutines tasks, on the other hand, will not be automatically terminated; instead, they must be canceled on a specified lifetime. We may also declare canceling in conjunction with startup, as seen below.
Job by lifecycle for a private val job lifecycleScope.launchWhenCreated.aware / call suspend onDestroy cancel() / When the lifecycle is destroyed, cancel it. .lazy()
Using the launchOnStarted, launchOnCreated, and launchOnResume functions, we can simplify the preceding scripts to the following.
employment by launch for private val OnStarted / suspend.onDestroy cancel() is called. lazy()
If we need to gather a single flow from the coroutines lifetime scope, we may use the following code.
task by launchOnStarted(repository.fetchesDataFlow()) / collected value from repository.fetchesDataFlow().onDestroy cancel().lazy ()
addOnRepeatingJob
In the latest version of the Lifecycle-runtime-ktx, the addRepeatingJob extension has been introduced.
When this Lifecycle is invoked, it launches and executes the specified block in a coroutine. The owner’s lifecycle is now at a standstill. When the lifecycle state goes below the state, the launched coroutine will be terminated.
We can collect a flow on the coroutines lifecycle scope and automatically terminate it if the lifecycle falls below that state, and resume it if the lifecycle falls below that state again.
addOnRepeatingJob(Lifecycle.State.private val job: Job by addOnRepeatingJob(Lifecycle.State.private val job: Job by addOnRepeatingJo CREATED, fetchesDataFlow()) / value obtained from fetchesDataFlow() lazy()
Do you find this library to be useful?
Support it by becoming a member of the repository’s stargazers. ⭐
And keep an eye out for my future projects!
Skydoves 2024 (Creative Commons) (Jaewoong Eum) This file is licensed under the Apache License, Version 2.0 (the “License”); you may not use it unless you agree to the terms of the License. The License is available at http://www.apache.org/licenses/LICENSE-2.0. Unless otherwise required by relevant law or agreed upon in writing, software distributed under the License is provided “AS IS,” with no explicit or implied warranties or conditions. The exact wording regulating rights and restrictions under the L may be found in the License.
GitHub
https://github.com/skydoves/Lazybones
This is an example of lazy and fluent syntactic sugar for initializing Android lifecycle-aware properties. The android hilt github is a library that does this.
Related Tags
- alkaa github
- skydoves bundler
- floating view android github
- material spinner android github
- transformation layout