Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Otto integration #272

Closed
mandrachek opened this Issue · 9 comments

4 participants

@mandrachek

I'm using Otto, the bus library from square. I have a couple of methods annotated with @Subscribe, which is part of Otto.

When I add @Trace, it appears that the @Subscribe annotation isn't carried over, so when I post events those methods listen for, nothing happens. When I remove @Trace, they fire off as expected.

@EActivity(R.layout.login)
public class LoginActivity extends Activity {
    @ViewById
    protected EditText username;

    @ViewById
    protected EditText password;

    @ViewById
    protected Button loginButton;

    @Click({ R.id.loginButton })
    public void sendLogin() {
        Credentials credentials = new Credentials(
            username.getText().toString(), 
            password.getText().toString()
        );
        new LoginTask().run(credentials);
    }

    @Override
    public void onResume() {
        super.onResume();
        BusProvider.getInstance().register(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        BusProvider.getInstance().unregister(this);
    }

    // @Trace - won't work here (doesn't preserve @Subscribe?)
    @Subscribe
    public void onLoginFailure(LoginFailureEvent event) {
        Log.w(LoginActivity.class.getName(),"Login Failure");
    }

    // @Trace - won't work here (doesn't preserve @Subscribe?)
    @Subscribe
    public void onLoginSuccess(LoginSuccessEvent event) {
        Log.w(LoginActivity.class.getName(),"Login Success");
    }

}

@EBean
public class LoginTask {    
    @Background
    public void run(Credentials credentials) {
        // perform login here
        post(true);
    }

    @UiThread
    public void post(boolean success) {
        if (success)
            BusProvider.getInstance().post(new LoginSuccessEvent());        
        else
            BusProvider.getInstance().post(new LoginFailureEvent());        
    }
}
@mandrachek

Just FYI, I wound up taking a look at the code generated for the LoginTask_. I ended up dropping the annotations and creating my own version that doesn't hold a reference to the Context (Activity). Might be worthwhile to have @Background(useContext = false) or something.

@naixx

@mandrachek btw, how did you manage to register your LoginActivity? LoginActivity_ should not have any @Subscribe annotations, as I understand

@naixx

You can check square/otto#15
It is easier in Otto to fix this

@mandrachek

I switched to EventBus, which supposedly has better performance, since it doesn't rely on annotations. I forgot to add a call to register in onCreate, which may have been a problem too.

@pyricau
Owner

Just wondering.. do you guys think it'd be interesting to reproduce Otto features in AndroidAnnotations ? Either write an integration layer, or reproduce the features with compile time generation of the features (no class parsing) ?

Thinking about a compile time bus event, here are the features I'd see :

@Subscribe and @Produces usage would lead to automatic registration of associated instances. This means, however, that lifecycle must be handled careful (ie register in onResume() and unregister in onPause()).

Then you need to be able to inject a bus instance. Whether there should be only one bus instance, or multiple onces with identifiers, is also a good question.

@RomainPiel

I know it's an old entry but I'd love this to be integrated to Android Annotations.

@pyricau pyricau was assigned
@pyricau
Owner

Me too :)

@pyricau
Owner

I think we shouldn't reimplement this, but just use Otto, which works fine.

A future version of Otto is supposed to work with annotation processing. But it's not there yet, and I don't know if it will come for sure.

In the meantime, the only thing we need to do is to override the annotated methods in the generated subclass and delegate the calls to super.

Note: I updated the title of this issue

@pyricau pyricau referenced this issue from a commit
@pyricau pyricau Otto integration: @Subscribe & @Produce methods are overriden to enab…
…le scanning of the subclass #272
e22ca8e
@pyricau pyricau referenced this issue
Merged

Otto integration #526

@pyricau pyricau closed this in 2aa295e
@RomainPiel

Interesting! Now I'm using EventBus but I could switch to Otto as it's coming with aa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.