Using Otto.Bus Post and @subscribe is not being called

880 views Asked by At

I am trying to get @Subscribe to call back to the activity from an AsyncTask. I have read through the Otto technical documentation and have also read many of different articles online as well as here on stack and none of them seem to help me solve my issue.

The AsynTask call perfectly and is noted in my Log and I can even get the response from the post request. As well I am registering otto and unregistering it as I have seen in other questions and the technical documentation.

I execute my task by passing in the appropriate params from the SignupActivity:

new SignupTask(SignupActivity.this,
                                (questionPosition + 1),
                                response.getEditText().getText().toString(),
                                FirstName.getEditText().getText().toString(),
                                LastName.getEditText().getText().toString(),
                                Mail.getEditText().getText().toString(),
                                Pass.getEditText().getText().toString(),
                                "0.0.0.0").execute();

My SignupTask then makes an Async call:

 @Override
public Integer call() throws Exception {
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
    client.getAPIInterface().SignUp(new TaskRequests.SignUpRequest(firstNameString,
            lastNameString,
            email,
            pass,
            pass),new Callback<String>() {
        @Override
        public void success(String msg, Response response) {
            SignupResponse resp = new SignupResponse();
            resp.setMsg(msg);
            OttoBusSingleton.getInstance().post(resp);
        }

Now on the SignupActivity i have a method to handle with Otto via @Subscribe:

@Subscribe
public void onSignUpSuccess(SignupResponse event) {
    loading.dismiss();

    if(!BuildConfig.DEBUG_MODE) {
        Log.i(TAG, "!BuildConfig.DEBUG_MODE : " + AnswersAnalytics.SIGN_UP_PATIENT);
        Answers.getInstance().logCustom(new CustomEvent(AnswersAnalytics.SIGN_UP));
    }

    Toast.makeText(this,
            event.getMsg(),
            Toast.LENGTH_LONG).show();
    this.finish();
}

From here I am trying to have the Otto callback handle the success on the SignupActivity and it is never hit when placing a breakpoint.

I have made sure the imports are properly there and I can't seem to place my finger on the possible solution.

1

There are 1 answers

5
ישו אוהב אותך On BEST ANSWER

In order to receive events, a class instance needs to register with the bus.

You need to add the following something like the following code to your activity (taken from the sample):

...
@Override 
protected void onResume() {
super.onResume();

    // Register ourselves so that we can provide the initial value.
    BusProvider.getInstance().register(this);
}

@Override 
protected void onPause() {
  super.onPause();

  // Always unregister when an object no longer should be on the bus.
  BusProvider.getInstance().unregister(this);
}
...

Please be noted that you need to try registering the bus in onCreate() and unregistering in onDestroy(). This is because when Activity in inactive the onPause() will be called. So you will accidentally unregistering the bus.