Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Otto integration #272

Closed
Closed

Description

@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());        
    }
}

Activity

  1. mandrachek commented on Jul 13, 2012

    @mandrachek
    Author

    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.

  2. naixx commented on Jul 27, 2012

    @naixx
    Contributor

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

  3. naixx commented on Jul 28, 2012

    @naixx
    Contributor

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

  4. mandrachek commented on Jul 29, 2012

    @mandrachek
    Author

    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.

  5. pyricau commented on Aug 9, 2012

    @pyricau
    Contributor

    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.

  6. romainpiel commented on Feb 19, 2013

    @romainpiel

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

  7. ghost assigned on Feb 27, 2013
  8. pyricau commented on Feb 27, 2013

    @pyricau
    Contributor

    Me too :)

  9. pyricau commented on Feb 27, 2013

    @pyricau
    Contributor

    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

  10. added a commit that references this issue on Mar 5, 2013
  11. romainpiel commented on Mar 5, 2013

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions