我在我的项目中使用 Roboguice 进行 DI。根据 android 文档,只能存在一个应用程序实例。但是由 OS 和 Roboguice 创建的实例是不同的。
如何强制 Roboguice 注入操作系统创建的应用程序并禁用新实例的创建?
一些说明以下情况的代码
public class MyApplication extends Application {
public static MyApplication getInstance() {
if (instance == null) {
throw new IllegalStateException("Application isn't initialized yet!");
}
return instance;
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
}
public class MyActivity extends RoboActivity {
// roboApp and osApp two different objects but expected that roboApp the same as osApp
@Inject
private MyApplication roboApp;
private MyApplication osApp = MyApplication.getInstance();
}