SlideShare a Scribd company logo
EventBus for Android
Ting Cheng
Intent, Handler,
Broadcast
Status Quo in Android
Fragment Fragment
Activity
Service / Helper
Thread
Activity
Issues !
• Define interfaces
• Callbacks for async
• Listener management
• Propagation through all layers
EventBus
Fragment Fragment
Activity
Service / Helper
Thread
Activity
Event
Bus
Three things
• Event
• Subscriber
• Publisher
EventBus in 4 Steps
1. Define an event
public class MyEvent {}
2. Register subscriber
EventBus.getDefault().register(this);
3. Post an event
EventBus.getDefault().post(event);
4. Receive the event
public void onEvent(MyEvent event);
Publish / Subscribe
Publisher
Event
Bus
Subscriber
Event
post()
Event
onEvent()
Subscriber
Event
onEvent()
• EventBus instances
• It anywhere in your code
• Event handler methods
• Naming convention: onEvent
• public visibility
• No return value (void)
• Single parameter for the event to receive
Event Type is a Filter
Publisher
Event
Bus
Subscriber
Event
post
(MyEvent)
Event
onEvent(MyEvent)
Subscriber
onEvent(OtherEvent)
Code: Sender
MyEvent myEvent = new MyEvent(anyData);
EventBus.getDefault().post(myEvent);
Code: Receive
MyActivity extends Activity
// onCreate or onResume
EventBus.getDefault().register(this);
// onDestroy or onPause
EventBus.getDefault().unregister(this);
public onEvent(MyEvent event) { … }
Example:
Fragment to Fragment
ListFragment DetailFragment
Activity
DetailFragment
// Here‘s the action. How is it called?
public void loadDetails(Item item) {
…
}
ListFragment
interface Contract {
void onItemSelected(Item item);
}
// Propagate to Activity
((Contract)getActivity()).onItemSelected(item);
Activity
public class MyActivity implements ListFragment.Contract {
public void onItemSelected(Item item) {
DetailFragment detailFragment = (DetailFragment)
getFragmentManager().findFragmentBy…;
detailFragment.loadDetails(item);
}
}
EventBus Example
Event class
public class ItemSelectedEvent {
public final Item item;
public ItemSelectedEvent(Item item) {
this.item = item;
}
}
Post / Receive
// In ListFragment
EventBus.getDefault().post(new ItemSelectedEvent(item));
// In DetailFragment (extends EventBusFragment)
public void onEvent(ItemSelectedEvent event) {
…
}
Thread Modes
• PostThread: Direct call in same thread
• MainThread: UI updates etc.
• BackgroundThread: „not the main thread“
• Async: always asynchronous to posting
• Used in the method name: onEventXXX
• No additional code required
Tips
Sticky Event
• Events can be posted using sticky mode
postSticky(event); // Instead of post(…)
• The last sticky event is kept in memory
• Registration with getting sticky events
registerSticky(subscriber);
• Query for sticky event
getStickyEvent(Class<?> eventType)
• Remove sticky event (Class or Object)
removeStickyEvent(Class<?> eventType)
When to use EventBus
• Medium/high distance of components:
• Potentially multiple dependencies
• Asynchronous logic
• When not to use EventBus
• Strictly internal matters:
• Use methods and interfaces instead
• Inter-process communication

More Related Content

Similar to Event bus for android

Java event processing model in c# and java
Java  event processing model in c# and javaJava  event processing model in c# and java
Java event processing model in c# and javaTech_MX
 
20141107 node js_eventemitterpattern_anney
20141107 node js_eventemitterpattern_anney20141107 node js_eventemitterpattern_anney
20141107 node js_eventemitterpattern_anneyLearningTech
 
004 - JavaFX Tutorial - Event Handling
004 - JavaFX Tutorial - Event Handling004 - JavaFX Tutorial - Event Handling
004 - JavaFX Tutorial - Event Handling
Mohammad Hossein Rimaz
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
Java gui event
Java gui eventJava gui event
Java gui event
SoftNutx
 
Event handling62
Event handling62Event handling62
Event handling62myrajendra
 
Azure notification hubs
Azure notification hubsAzure notification hubs
Azure notification hubs
Dinusha Kumarasiri
 
Ss2gx
Ss2gxSs2gx
Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.
Nida Ismail Shah
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
usvirat1805
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
teach4uin
 
Module3.11.pptx
Module3.11.pptxModule3.11.pptx
Module3.11.pptx
VeenaNaik23
 
Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Elizabeth Smith
 
Explained: Domain events
Explained: Domain eventsExplained: Domain events
Explained: Domain events
João Pires
 
Deep Inside Android Hacks
Deep Inside Android HacksDeep Inside Android Hacks
Deep Inside Android Hacks
Keishin Yokomaku
 
Module 5.pptx
Module 5.pptxModule 5.pptx
Module 5.pptx
VeenaNaik23
 
Broadcast receivers
Broadcast receiversBroadcast receivers
Broadcast receivers
maamir farooq
 
Event and signal driven programming
Event and signal driven programmingEvent and signal driven programming
Event and signal driven programmingElizabeth Smith
 
EventHandling.pptx
EventHandling.pptxEventHandling.pptx
EventHandling.pptx
VamsiKrishna424209
 

Similar to Event bus for android (20)

Java event processing model in c# and java
Java  event processing model in c# and javaJava  event processing model in c# and java
Java event processing model in c# and java
 
20141107 node js_eventemitterpattern_anney
20141107 node js_eventemitterpattern_anney20141107 node js_eventemitterpattern_anney
20141107 node js_eventemitterpattern_anney
 
004 - JavaFX Tutorial - Event Handling
004 - JavaFX Tutorial - Event Handling004 - JavaFX Tutorial - Event Handling
004 - JavaFX Tutorial - Event Handling
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
 
Java gui event
Java gui eventJava gui event
Java gui event
 
Event handling62
Event handling62Event handling62
Event handling62
 
Azure notification hubs
Azure notification hubsAzure notification hubs
Azure notification hubs
 
Ss2gx
Ss2gxSs2gx
Ss2gx
 
Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
Module3.11.pptx
Module3.11.pptxModule3.11.pptx
Module3.11.pptx
 
Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012
 
Explained: Domain events
Explained: Domain eventsExplained: Domain events
Explained: Domain events
 
Deep Inside Android Hacks
Deep Inside Android HacksDeep Inside Android Hacks
Deep Inside Android Hacks
 
Module 5.pptx
Module 5.pptxModule 5.pptx
Module 5.pptx
 
Broadcast receivers
Broadcast receiversBroadcast receivers
Broadcast receivers
 
Event and signal driven programming
Event and signal driven programmingEvent and signal driven programming
Event and signal driven programming
 
EventHandling.pptx
EventHandling.pptxEventHandling.pptx
EventHandling.pptx
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 

Recently uploaded

Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 

Recently uploaded (20)

Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 

Event bus for android