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.
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):
Please be noted that you need to try registering the bus in
onCreate()
and unregistering inonDestroy()
. This is because when Activity in inactive theonPause()
will be called. So you will accidentally unregistering the bus.