SlideShare ist ein Scribd-Unternehmen logo
1 von 24
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

Weitere ähnliche Inhalte

Ähnlich wie 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 HandlingMohammad Hossein Rimaz
 
Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
 
Event handling62
Event handling62Event handling62
Event handling62myrajendra
 
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.pptxusvirat1805
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handlingteach4uin
 
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 eventsJoão Pires
 
Event and signal driven programming
Event and signal driven programmingEvent and signal driven programming
Event and signal driven programmingElizabeth Smith
 

Ähnlich wie 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
 

Kürzlich hochgeladen

11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdfHafizMudaserAhmad
 
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectssuserb6619e
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solidnamansinghjarodiya
 
Configuration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentConfiguration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentBharaniDharan195623
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxNiranjanYadav41
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectErbil Polytechnic University
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptbibisarnayak0
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 

Kürzlich hochgeladen (20)

Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf
 
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solid
 
Configuration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentConfiguration of IoT devices - Systems managament
Configuration of IoT devices - Systems managament
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptx
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction Project
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.ppt
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 

Event bus for android