Popularity
1.4
Growing
Activity
0.0
Stable
22
4
4

Description

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

Programming language: Kotlin
License: Apache License 2.0
Latest version: v1.0.1

LifecycleEvents alternatives and similar packages

Based on the "EventBus" category.
Alternatively, view LifecycleEvents alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of LifecycleEvents or a related project?

Add another 'EventBus' Package

README

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.io</id>
    <url>https://jitpack.io</url>
    </repository>
</repositories>
Add the dependency
<dependency>
   <groupId>com.github.RobertApikyan</groupId>
   <artifactId>LifecycleEvents</artifactId>
   <version>1.0.1</version>
</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.


*Note that all licence references and agreements mentioned in the LifecycleEvents README section above are relevant to that project's source code only.