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 can I update Task status and the Actuals of a specific task?
using prolog & clpr for a system of constraints
Remove characters from consecutive runs of the same character in a String using Java 1.8
How to focus on currently selected item in Listview in android
Does equals apply on itself when looking an element in a list?
Java PriorityQueue behaviour with 3 elements in the Queue [duplicate]
Unable to resolve place holder in Mockito Unit test
WebParam.Mode.OUT meaning and documentation
Gradle runs all tests when only a few tests changes
Android Firabse Cloud Message GoogleSignatureVerifier: com.google.android.gms signature not valid
HtmlElement not showing child nodes but HtmlElement.asXml() method does
Spark steaming read from Kafka and apply Spark SQL aggregations in Java
Trouble understanding constructors and reasons for calling them in another class
simulated annealing for solving concentrator location prob
Is postfix self assignment behaviour strictly defined in C#/Java?
How to read a file from Amazon S3 Using Apache Spark without writing it in local file?

Categories

HOME
ionic-framework
openlayers
clips
blogger
netbeans
json.net
cplex
rsync
maven-3
tizen-web-app
alpha
upload
google-translate
normalizr
oxyplot
autocad-plugin
css-animations
telerik-reporting
poltergeist
react-css-modules
tooltipster
typo3-6.2.x
xacml
elasticsearch-net
bpel
unboundid
lightswitch-2013
media-queries
google-rich-snippets
nameservers
junit5
key-value-observing
create-table
perlin-noise
rotational-matrices
environment-modules
nssegmentedcontrol
bower-install
qwerty
scaffold
mixture-model
abstract-class
ssjs
webdriver-manager
keydown
babel-core
osx-mavericks
python-c-api
aurelia-validation
diagnostics
android-cursor
namecoin
arrow-keys
trash
ctest
autorest
gridview-sorting
react-native-listview
dlna
livequery
historian
freedesktop.org
rtbkit
srand
jms2
spidermonkey
wyam
varargs
sframe
map-projections
ami
streambase
lustre
python-green
fscommand
ng-animate
network-interface
android-imagebutton
titanium-modules
contenttype
resty-gwt
oam
ms-project-server-2010
gridfs
tidy
apc
opcache
ril
eventual-consistency
seed
comaddin
eventlistener
distutils
broken-links
first-responder
propertyeditor
dsn
armcc
windows-phone-7.1.1
bigcouch
work-stealing
winbugs14
html-input
site.master
jquery-ui-droppable
mirah
premature-optimization
wsdl.exe
caching-application-block

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App