LifecycleEvents library is an event bus implementation, using lifecycle from android architecture components and kotlin language features

Overview

N|Solid

MinSDK 14+

License

LifecycleEvents

LifecycleEvents library is an event bus implementation, using lifecycle from android architecture components and kotlin language features. It is also designed for Java language.

Simple Usage

LifecycleEvents allows us to send any object as an event, In this example, UserInfo's instance will be send as an event.

kotlin
...
// Sending userInfo as an event
userInfo.sendAsEvent() // sendAsEvent is a kotlin extention method, for java user Events.sendEvent(userInfo) mthod
...

// Receiving userInfo
disposable = observeEvent<UserInfo> { userInfo ->
            // use userInfo object here
        }
...

// Cancel event observation
disposable.dispose()
...
java
...

// Sending userInfo as an event
Events.sendEvent(userInfo);
...

// Receiving userInfo
disposable = Events.observeEvent(UserInfo.class, userInfo ->{
	// use userInfo object here
});
...

// Cancel event observation
disposable.dispose()
...

With Android Lifecycle

While observing events inside Activity or Fragment we do not need to cancel event observation manually, we can just pass lifecycleOwner instance as an observeEvent method's argument, and dispose() method will be called automatically with onDestroy() lifecycle method.

kotlin
observeEvent<User>(lifecycleOwner: this) { userInfo ->
    // use userInfo object here
}

Pending Events Handling

Observation with lifecycleOwner allows us to handle events only when activity or fragment are at list started but not stopped. Events that are arrived when activity/fragment is in the stopped state will be marked as pending events, and will be delivered with onStart() lifecycle method. By default pending events will delivered with the same receiving order, but we can change delivery behavior by setting observeEvent method's rule attribute.

kotlin
// After onStart() Only the last event will be delivered
observeEvent<User>(this, PendingEventsRules.ONLY_LAST) { userInfo ->
    // use userInfo object here
}

There are five types of PendingEventsRules

1. IGNORE
2. IN_ORDER // default
3. REVERSE_ORDER
4. ONLY_LAST
5. ONLY_FIRST
6. IMMEDIATE // events will be delivered, even after onStop()

Threads Handling

By default all events will be sent and received on the main thread, but we can change this. In the above example, event observation will be done on the background worker thread (do this if you have big number of observers for specified event), and the event will be received on the main thread.

kotlin
...
// observers will be iterated on the background worker thread
user.sendAsEvent(Threads.BACKGROUND)
...
// event will be recieved on the main thread (here you can not specify Threads.MAIN, it is the default value)
observeEvent<User>(this, Threads.MAIN) { userInfo ->
    // use userInfo object here
}

// also you can specify  Threads.BACKGROUND, in order to receive events on the background worker thread
observeEvent<User>(this, Threads.BACKGROUND) { userInfo ->
    // use userInfo object here
}

Add to Project

Add to project level build.gradle
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
Add to app module level build.gradle
dependencies {
    implementation 'com.github.RobertApikyan:LifecycleEvents:1.0.1'
}
Maven
<repositories>
    <repository>
    <id>jitpack.ioid>
    <url>https://jitpack.iourl>
    repository>
repositories>
Add the dependency
<dependency>
   <groupId>com.github.RobertApikyangroupId>
   <artifactId>LifecycleEventsartifactId>
   <version>1.0.1version>
dependency>

View Robert Apikyan profile on LinkedIn

License

Copyright 2018 Robert Apikyan

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
Alkaa is a to-do application project to study the latest components, architecture and tools for Android development
Alkaa is a to-do application project to study the latest components, architecture and tools for Android development

Alkaa (begin, start in Finnish) is a to-do application project to study the latest components, architecture and tools for Android development. The project evolved a lot since the beginning is available on Google Play! ❤️

Small training project where dagger, dagger hilt and other components are used for clean architecture
Small training project where dagger, dagger hilt and other components are used for clean architecture

LeagueNow 🏆 LeagueNow is a sample soccer team list Android application 📱 built to demonstrate use of modern Android development tools - (Kotlin, Arc

A quiz app built with trivia api. This app was built with mvvm architecture, dagger-hilt, retrofit, room database, and navigation components.
A quiz app built with trivia api. This app was built with mvvm architecture, dagger-hilt, retrofit, room database, and navigation components.

A quiz app built with trivia api. This app was built with mvvm architecture, dagger-hilt, retrofit, room database, and navigation components.

App made using Kotlin to retrieve data from an API and show in a recyclerview with Login and SignUp features
App made using Kotlin to retrieve data from an API and show in a recyclerview with Login and SignUp features

App made using Kotlin to retrieve data from an API and show in a recyclerview with Login and SignUp features.

MVVM News Application with clean code architecture & android jetpack components.
MVVM News Application with clean code architecture & android jetpack components.

Android - Clean Architecture - Kotlin The purpose of this repo is to follow up Clean Architecture principles by bringing them to Android. The repo con

GithubBrower - Github Browser Sample with Android Architecture Components

Github Browser Sample with Android Architecture Components This is a sample app

Movie application created with TMDB API, Architecture Components, Android Jetpack libraries

MovieApp Movie application created with TMDB API, Architecture Components, Android Jetpack libraries Built With 🛠️ • Kotlin - The language used in th

A general purpose kotlin library that use kotlin coroutines, flows and channels to provide timer features with the most easy and efficient way

Timer Timer is a general purpose kotlin library that use kotlin coroutines, flows and channels to provide timer features with the most easy and effici

A showcase music app for Android entirely written using Kotlin language

Bandhook Kotlin This project is a small replica of the app I developed some time ago. Bandhook can still be found on Play Store At the moment it will

Comments
  • Library does not load

    Library does not load

    Gradle APPLICATION: repositories { google() jcenter() maven { url 'https://jitpack.io' } }

    Gradle APP: implementation 'com.github.RobertApikyan:LifecycleEvents:0.0.1'

    ERROR: Error:(29, 20) Failed to resolve: com.github.RobertApikyan:LifecycleEvents:0.0.1 Show in File
    Show in Project Structure dialog

    opened by RafaelBarbosatec 2
Releases(1.0.2)
Owner
Robert
Robert
Android News App built in kotlin with implementation of MVVM architecture, android navigation components and retrofit. Displays news to users allowing them to share and save news.

News-App Android news app built in kotlin that fetches news data from news api with Retrofit and displays news to users. This App follow MVVM architec

Raj Manjrekar 16 Dec 29, 2022
It is a NBAApp developed by Kotlin. It uses MVVM design pattern, Coroutines, Retrofit and JetPack libraries like Room, Lifecycle, ViewBinding, DataBinding, Hilt and Navigation.

NbaApp It is a NBAApp developed by Kotlin. It uses MVVM design pattern, Coroutines, Retrofit and JetPack libraries like Room, Lifecycle, ViewBinding,

Tuna Ateş Koç 2 Feb 26, 2022
:movie_camera: Movie discovery app showcasing Android best practices with Google's recommended architecture: MVVM + Repository + Offline support + Android Architecture Components + Paging library & Retrofit2.

Popular Movies Stage 1 + Stage 2 Discover the most popular and top rated movies playing. Movies data fetched using themoviedb.org API. ✨ Screenshots M

Yassin AJDI 189 Nov 26, 2022
DessertPusher - A dessert app displaying the functionalities of activity lifecycle and fragments

Activity Lifecycle - DessertPusher This is the toy app for lesson 4 of the Andro

Brian Kubai 0 Jan 18, 2022
Weather application example with Android Architecture components and Clean Architecture

Weather application example with Android Architecture components and Clean Architecture Weather app that shows how to architect an android app in a cl

null 2 Dec 3, 2021
This Android app shows bus connections from Koleje Strahov station to Dejvická station and the other way in the city of Prague

This Android app shows bus connections from Koleje Strahov station to Dejvická station and the other way in the city of Prague. These are important for many students from the Czech Technical University in Prague.

Lašťa Apps 3 Jul 28, 2022
🛒A Minimal Expense E-Commerce App built to demonstrate the use of modern android architecture components [Navigation, Room, MotionLayout, etc..] with MVVM Architecture. ✔

E-Store A Simple E-Commerce App ?? built to demonstrate the use of modern android architecture component with MVVM Architecture ?? . Made with love ❤️

Ameen Essa 14 Nov 3, 2022
📚 Sample Android Components Architecture on a modular word focused on the scalability, testability and maintainability written in Kotlin, following best practices using Jetpack.

Android Components Architecture in a Modular Word Android Components Architecture in a Modular Word is a sample project that presents modern, 2020 app

Madalin Valceleanu 2.3k Jan 3, 2023
Patter Lock using Hilt, Coroutines, Flow and Custom View Components based on MVVM architecture.

Pattern Lock App Sample project for created Pattern Lock View using custom view. Preview Usage Step 1 Add the PatterLockView in your XML layout file.

Furkan Özcan 5 Aug 22, 2021
Movie Android App written in Kotlin, MVVM, RxJava, Coroutine (Upcoming), Android Architecture Components and Jetpack Compose (Upcoming).

MovieHunt MovieHunt is a sample Android project using The Movie DB API based on MVVM architecture. It showcases the latest Android tech stacks with we

Engine Bai 596 Dec 31, 2022