4

有没有办法在不使用 RoboActivity 扩展 Activity 类的情况下使用 roboguice。

4

2 回答 2

10

是的。使用尚未测试版的 1.2-SNAPSHOT 会更容易。要使用 1.2,只需将以下内容添加到您的 onCreate()、onContentChanged() 和 onDestroy()。如果您不使用 roboguice 事件,则不需要有关 EventManager 的信息:

@Override
protected void onCreate(Bundle savedInstanceState) {
    RoboGuice.getInjector(this).injectMembersWithoutViews(this);
    super.onCreate(savedInstanceState);
}

@Override
public void onContentChanged() {
    super.onContentChanged();
    RoboGuice.getInjector(this).injectViewMembers(this);
}


@Override
protected void onDestroy() {
    try {
        RoboGuice.destroyInjector(this);
    } finally {
        super.onDestroy();
    }
}

如果您使用的是 RoboGuice 1.1.x(最新的稳定版本),则原理相同,但调用略有不同。查看1.1 RoboActivity 源代码,了解您需要进行哪些调用。

于 2011-10-02T15:25:27.953 回答
0

它有效,但您必须实现 RoboContext 并声明它

protected HashMap<Key<?>,Object> scopedObjects = new HashMap<>();
于 2016-05-04T18:19:43.810 回答