Search code examples
androidroboguice

using roboguice without extending Activity


Is there a way to use roboguice without extending Activity class with RoboActivity.


Solution

  • Yes. It's easier with the 1.2-SNAPSHOT which isn't yet in beta. To use 1.2, just add the following to your onCreate(), onContentChanged(), and onDestroy(). You don't need the bits about the EventManager if you're not using roboguice events:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        RoboGuice.getInjector(this).injectMembersWithoutViews(this);
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public void onContentChanged() {
        super.onContentChanged();
        RoboGuice.getInjector(this).injectViewMembers(this);
    }
    
    
    @Override
    protected void onDestroy() {
        try {
            RoboGuice.destroyInjector(this);
        } finally {
            super.onDestroy();
        }
    }
    

    If you're using RoboGuice 1.1.x (the latest stable build), then the principle is the same but the calls are slightly different. Take a look at the 1.1 RoboActivity source to see which calls you need to make.