Skip to content

Instantly share code, notes, and snippets.

@alexoro
Created December 1, 2013 21:22
Show Gist options
  • Save alexoro/7740965 to your computer and use it in GitHub Desktop.
Save alexoro/7740965 to your computer and use it in GitHub Desktop.
public class AppInstance extends Application {
private static AppInstance mInstance;
@Inject
IStorage mStorage;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
RoboGuice.setBaseApplicationInjector(this, RoboGuice.DEFAULT_STAGE, RoboGuice.newDefaultRoboModule(this), new IOCModule(this));
mStorage.getInt();
}
public static AppInstance getInstance() {
return mInstance;
}
}
public class IOCModule extends AbstractModule {
private final AppInstance context;
@Inject
public IOCModule(final Context context)
{
super();
this.context = (AppInstance) context;
}
@Override
protected void configure() {
// bind(AppInstance.class).toInstance(context);
bind(IStorage.class).to(Storage1.class);
}
}
public interface IStorage {
int getInt();
}
public class Storage1 implements IStorage {
private int i = 0;
@Override
public int getInt() {
return i++;
}
}
public class POJO {
@Inject
public IStorage storage;
public POJO() {
RoboGuice.getInjector(AppInstance.getInstance()).injectMembers(this);
}
}
public class MainActivity extends RoboActivity {
@Inject IStorage mStorage;
@Inject LocationManager mLocationManager;
@Inject protected Injector injector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
setContentView(tv);
// RoboGuice.getInjector(AppInstance.getInstance()).injectMembers(this);
POJO pojo = new POJO();
tv.setText("" + pojo.storage.getInt());
tv.setText("" + pojo.storage.getInt());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment