将数据从活动发送到Android中的类(Send Data from an Activity to a class in Android)

编程入门 行业动态 更新时间:2024-10-11 03:25:34
将数据从活动发送到Android中的类(Send Data from an Activity to a class in Android)

我有这个问题:我有一个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 }

更多推荐

本文发布于:2023-07-29 05:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1313034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   数据   Android   Send   Activity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!