我想知道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
只是一個空類的方式。