subscriber does not get posted message from eventB

2019-08-15 03:32发布

I want to use eventbus into my app. for that I add eventbus lib to my gradle.then I have created a class like this:

public class UserEvent {

private User eUser; // esup user model
private String domain;
private String securitySrv;
private Cookie cookie;
private int totalUsersSecurity;
private ExecutorService executorService;
// constructor and getter

then into main fragment I have added this lines and finally I have posted my message:

UserEvent userEvent = new UserEvent(eUser, domain, securitySrv, cookie,
totalUsersSecurity, executorService);
EventBus.getDefault().post(userEvent);

into main fragment I have ViewPager that this pager have loaded another fragment called AllUsersFragment.

In AllUsersFragment first I have registered eventbus:

EventBus bus = EventBus.getDefault();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
    bus.register(this);
    return rootView;
}

and then i wrote this method:

    @Subscribe
public void onEvent(UserEvent event) {
    securitySrv = event.getSecuritySrv();
    cookie = event.getCookie();
    totalUsersSecurity = event.getTotalUsersSecurity();
    executorService = event.getExecutorService();

}

into my resume method I want to pass UserEvent from eventbus to another class by its constructor:

@Override
public void onResume() {
    super.onResume();
    paginator = new Paginator(getContext(),totalUsersSecurity,executorService);

}

but totalUsersSecurity and executorService are null? while these variables was fill at main fragment.in debug mod at android studio this public void onEvent(UserEvent event) is not never called. thank you for helping?

I am using android studio 3 with gradle:3.0.0'

2条回答
爷、活的狠高调
2楼-- · 2019-08-15 03:54

Finally I fix this problem. according eventBus wiki

I just use Sticky Events .now I received my post

查看更多
迷人小祖宗
3楼-- · 2019-08-15 03:57

have you registered for the event on OnStart()

 @Override
 public void onStart() {
     super.onStart();
     EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
     super.onStop();
     EventBus.getDefault().unregister(this);
 }
查看更多
登录 后发表回答