我有这个问题:我有一个Activity和两个类MyGLSurfaceView,MyGLRenderer
在Activity中有一个生成不同数字的线程,我必须将此变量发送到MyGLRenderer。
我无法通过构造函数的参数传递此变量:/这是将变量从活动发送到类的不同模式吗? 我能怎么做 ?
I have this problem : I have one Activity and two class MyGLSurfaceView , MyGLRenderer
In Activity there is a thread that generates different numbers , and I have to send this variable to MyGLRenderer.
I can't pass this variable through constructor's arguments :/ THere is a different mode to send variable from an activity to a class ? How can I do ?
最满意答案
也许这不是一个非常明确的解决方案,但你可以考虑使用EventBus https://github.com/greenrobot/EventBus ,它可以解决某些特定于Android的特定情况。
在你的类构造函数中注册EventBus
EventBus.getDefault.register(this);并在您的活动onCreate()和onDestroy()方法中register() / unRegister() 。
当你需要从activity发布价值时
EventBus.getDefault().post(new WhateverObject());
并在其他课程中获得价值
public void onEvent(WhateverObject object){ //doing stuff }Maybe it's not really clear solution but you can consider using EventBus https://github.com/greenrobot/EventBus which feet to some restricted android-specific situations.
Register EventBus in you class constructor like
EventBus.getDefault.register(this);and register()/unRegister() in your activity onCreate() and onDestroy() methods.
When you need just post value from activity like
EventBus.getDefault().post(new WhateverObject());
And receive value in other class
public void onEvent(WhateverObject object){ //doing stuff }更多推荐
发布评论