开发者

using roboguice without extending Activity

Is there a way to use roboguice without extending Activity class with RoboActivi开发者_如何转开发ty.


Yes. It's easier with the 1.2-SNAPSHOT which isn't yet in beta. To use 1.2, just add the following to your onCreate(), onContentChanged(), and onDestroy(). You don't need the bits about the EventManager if you're not using roboguice events:

@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();
    }
}

If you're using RoboGuice 1.1.x (the latest stable build), then the principle is the same but the calls are slightly different. Take a look at the 1.1 RoboActivity source to see which calls you need to make.


It works, but you must implement RoboContext and declare this

protected HashMap<Key<?>,Object> scopedObjects = new HashMap<>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜