Eventex, Android Express Events
Android library to send/post data to Fragments, Layouts, Activity. No need to create interfaces and pass listeners to multiple classes. There is also no need to subscribe/unsubscribe for events! |
Try It Now
Make sure Java 8 (1.8) support is enabled in the gradle file
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Add EventEx to the project gradle file (Androidx based projects)
implementation 'dev.uchitel:eventex:2.1.0'
Or for Android Support Library projects
implementation 'dev.uchitel:eventex-support:2.0.0'
Simple
To post message
new UIEvent("button.ok.click").post(view); // yes, this is it
To receive message. In any class that extends Fragment, ViewGroup, or Activity
public class CustomFragment extends Fragment implements UIEventListener {
// .....
@Override
public boolean onMessage(@NonNull UIEvent uiEvent) {
switch (uiEvent.what) {
case "button.ok.click":
Log.d(uiEvent.toString());
return true; // to stop message propagation
}
return false; // to let other objects to process message
}
}
No need to setOnItemClickListener in the RecyclerView.Adapter! Much less boilerplate code compare to classic solution Communicate with other fragments! Class CustomFragment extends Android class Fragment. It will also work well if the class extends Activity, ViewGroup, or any layout derived from ViewGroup (LinearLayout, FrameLayout, etc..)
Features
- Delivers messages between UI components of an Activity.
- Supports synchronous and asynchronous communication.
- No need to subscribe/unsubscribe to receive messages.
- Can deliver any data type.
- Completely decouples components.
- No reflection and no ProGuard rules.
- Tiny code size.
More Details
Message can be sent synchronously
new UIEvent(12345).send(viewGroup);
Message can carry additional integer, string value, and anything that can fit into Bundle:
new UIEvent(12345)
.setText("some text to pass with message")
.setNumber(9876) // some integer to pass with message
.putAll(bundle)
.post(viewGroup);
Next code will properly receive this message:
public class FragmentReceiver extends FrameLayout implements UIEventListener {
// .....
@Override
public boolean onMessage(@NonNull UIEvent uiEvent) {
switch (uiEvent.code) {
case 12345:
Log.d("FragmentReceiver", "text="+uiEvent.getText());
Log.d("FragmentReceiver", "number="+uiEvent.getNumber());
return true; // to stop message propagation
}
return false; // to let other components to process the message
}
}
Class UIEvent isn't 'final' and can be extended to carry any data. See sample CustomUIEvent.
Message can use integer ID, string ID, or both for more complex control scenarios:
new UIEvent(12345, "button.ok.click"))
.post(view);
The 'onMessage' for the above code:
public class FragmentReceiver extends Activity implements UIEventListener {
// .....
@Override
public boolean onMessage(@NonNull UIEvent uiEvent) {
switch (uiEvent.code) {
case 12345:
if(uiEvent.what.equals("button.ok.click")){
// ...
return true; // to stop message propagation
}
}
return false; // to let other components to process the message
}
}
When writing android library make sure to use 'namespace' to prevent collisions. Sending message inside library can look like:
new UIEvent("button.ok.click")
.setNamespace("lib_name.company_name.com")
.post(view);
Namespace "lib_name.company_name.com" is going to prevent ID collisions when the library is distributed to third party developers.
And to receive this message inside library module
public class FragmentReceiver extends Fragment implements UIEventListener {
// .....
@Override
public boolean onMessage(@NonNull UIEvent uiEvent) {
// return false if this is not library message
if (!uiEvent.getNamespace().equals("libname.company.com")) return false;
switch (uiEvent.what){
case "button.ok.click":
Log.d(uiEvent.getText());
return true; // to stop message propagation
}
return false; // to let other objects to process message
}
}
Requirements
- Android 4.1.0(API 16) or above.
- Java 8
R8 / ProGuard
No special requirements for R8 or ProGuard
Do you think it might be useful? Help devs to find it.
Alternative libraries
License
Copyright 2019 Alexander Uchitel
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.