public class BeaconApplication extends Application {
public IGameMapLoader mapLoader;
public BeaconApplication () {
mapLoader = new GameMapLoader(this);
}
}
...then in my Activity:
public class BeaconActivity extends Activity {
IGameMapLoader gameMapLoader;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BeaconApplication ba = (BeaconApplication)getApplication();
this.gameMapLoader = ba.mapLoader;
}
Generally speaking, I don't like the fact that the Activity now needs to know about the Application, but I sure like it a helluva lot more than:
public class BeaconApplication extends RoboApplication {
protected void addApplicationModules(List
modules.add(new BeaconModule());
}
}
public class BeaconModule extends AbstractAndroidModule{
private Application application;
public BeaconModule(Application application) {
this.application = application;
}
protected void configure() {
IGameMapLoader mapLoader = new GameMapLoader(this.application);
bind(IGameMapLoader.class).toInstance(mapLoader);
}
}
public class BeaconActivity extends RoboActivity {
@Inject IGameMapLoader gameMapLoader;
}
I could go back and utilize a "Module" to construct and swap out depending on the environment, but for the time being I'm not coupled to all this extension and annotation nonsense. Also, I could use "get()" methods rather than instance variables on the Application class which could provide me with a way to lazyily create things (although in practice thats usually not a good idea), but anyways for normal operation, the tradeoff to use Guice/RoboGuice in an Android application just seems like a bad idea. (You pay alot for not so much).
No comments:
Post a Comment