java


EventBus communication between two Fragments. No subscriber error


I used EventBus for communication between Activity and Fragment without problems, however when I tired to do the same with two Fragments, EventBus notifies me that there has been no subscriber for given event. Here is a sample example of sending event from FragmentB to subscribed FragmentA:
Sample FragmentA(Receiver, which doesn't receive a message):
public class FragmentA extends Fragment {
private View mView;
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(int msg) {/* Do something */};
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.a_fragment_layout, container, false);
EventBus.getDefault().register(this);
return mView;
}
#Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
}
Sample FragmentB(Sender):
public class FragmentB extends Fragment {
private View mView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.b_fragment_layout, container, false);
sendMessage(1);
return mView;
}
private void sendMessage(int msg){
EventBus.getDefault().post(msg);
}
}
Error which I get:
No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
The problem seems to be quite simple, however I can't figure it out.
Well, create a simple model class, let's call it NotifyEvent
public class NotifyEvent {
public int mValue;
public NotifyEvent(int value){
this.mValue = value;
}
}
and now sending and receiving should be like
in FragmentB:
private void sendMessage(int msg){
EventBus.getDefault().post(new NotifyEvent(msg));
}
in FragmentA:
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(NotifyEvent event){
int msg = event.mValue;
// do something with msg.
}
for details please go here
I figured out my problem. Funny thing, but you are required to pass some MessageEvent object rather than int or another primitive type. What I have done is simply posted a custom MessageEvent object which contained required parameters instead of raw integer:
EventBus.getDefault().post(new SomeObject(9));
Instead of
EventBus.getDefault().post(9);
Example of POJO:
public class SomeObject {
private int mTime;
public SomeObject(int time){
this.mTime = time;
}
public int getTime() {
return mTime;
}
public void setTime(int time) {
this.mTime = time;
}
}
Hope it will help somebody!

Related Links

How to specify multiple input paths to a Dataflow job
How to reduce response time while using hibernate
How to use version in REST API with spring mvc?
How to Group Documents by a Field and Calculate Count in mongodb using java [closed]
Java: Applying probability constraints on Random() [closed]
Testng Absent Code attribute in method that is not native or abstract in class file
convert “Nested ArrayList XML” to java Object using Two Custom Converters of XStream
Java 8 Stream - check if instanceof
elastic search not x content exception
php script to java for TrafficFactory api
Unable to get JDBC connection with PostgreSQL
Java Enum data long value being set to zero
java xuggler leads to fatal error
How to notify client-side of an Instant-Message (IM) app of updates?
Android - Realm How to get value
Static body bridging two other static bodies

Categories

HOME
pdf
gerrit
activiti
octobercms
routes
sharepoint-designer
jgroups
jxls
icloud
networkx
azure-media-services
kentor-authservices
mapserver
resize
size
visual-studio-2005
fallback
opentracing
seaborn
graphlab
django-cms
viewport
conemu
web-sql
kvc
xpages-ssjs
sparse-matrix
spark-jobserver
chromebook
brunch
neo4j-spatial
primitive
tinymce-4
react-chartjs
protovis
gammu
restlet
android-browser
sharefile
angularjs-factory
opshub
temporary-files
azure-ml
mime
flashair
s
eclipse-gef
skeleton-css-boilerplate
jquery-validate
celery-task
sage-one
domain-model
integrity
nstextview
nativeapplication
linode
wdf
color-picker
savon
database-optimization
dstu2-fhir
mikroc
ctest
etsy
xml-attribute
autorest
simplewebrtc
prettytensor
rvest
bluemix-app-scan
system.management
freelancer.com-api
cartesian-product
libressl
skos
e10s
facebook-graph-api-v2.4
cctv
applescript-objc
elliptic-curve
valuechangelistener
dealloc
oracle-warehouse-builder
java.nio.file
multiprocessor
spring-io
dotnetnuke-5
jquery-mobile-dialog
orchardcms-1.7
type-equivalence
android-hardware
robotics-studio
gdata-api
word-processor
chrono
semantic-diff
work-stealing
libc++
nsobject
ecl
thunderbird-lightning
user-friendly
multi-tier
sef
avatar

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App