我想知道Roboguice注入的是哪种上下文,是应用程序上下文还是当前活动?roboguice注入哪个上下文?
我正在尝试使用Roboguice和Robospice。我在一个片段中注入了Robospice的SpiceManager
,但片段并不知道SpiceManager
,它通过一个接口来看它,假设我们说MyInterface
。
public class MyFragment extends RoboFragment {
//this is where the SpiceManager gets injected
@Inject MyInterface manager;
...
}
//this is the implementation that I'm going to inject
//it is simultaneously an event listener for the fragment's life cycle events so that the
//SpiceManager can be appropriately started and stopped.
public class MyManager implements MyInterface {
private SpiceManager spiceManager = new SpiceManager(MySpiceService.class);
//Which context will get injected here? How can I make Roboguice inject a specific context that I want, for example, a specific activity that I want.
private @Inject Context context;
//Here, I need to start the SpiceManager
public void myFragmentOnStart(@Observes OnStartEvent onStart) {
//SpiceManager requires a context, more specifically an activity which will be destroyed and then garbage collected, so It shouldn't be an application context because the resources SpiceManager uses will never be released.
spiceManager.start(context);
}
public void myFragmentOnStop(@Observes OnStopEvent onStop){
if (spiceManager.isStarted()) {
spiceManager.shouldStop();
}
}
}
我的问题是:
能RoboGuice观察活动事件旁片段事件,文件是不是清楚了吗?
我是否认为SpiceManager需要一个当片段/活动被销毁时会被销毁的上下文?我看了一下SpiceManager.start(Context context)
的代码,它创建了一个WeakReference
到Context
。
我该如何让RoboGuice注入特定的Context/Activity
?
是否有可能这样做,而没有MyFragment
知道它使用的MyInterface
对象需要Context
?
通过我发现OnStopEvent
有getActivity()
方法,所以没有问题越来越在onStop
的Activity
,但OnStartEvent
只是一个空类的方式。