android Java多个类更新活动UI(如何共享数据) [英] android Java Multiple classes update activity UI (how to share data)

查看:66
本文介绍了android Java多个类更新活动UI(如何共享数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经问过几次了,但是我似乎找不到合适的答案,或者有多种解决方案.我只是想寻找外观和最佳解决方案.

I know this question has been asked several times, but I cannot seems to find a suitable answer or there is multiple solutions. I am just trying to find were to look and the best possible solution.

我有一个应用程序,它具有多个活动,例如主",设置",用户配置文件"等,并且我有不同的信息来源,例如从服务器中提取数据,BLE通讯,背景计时器等.该计划将在单独的类中具有这些调用comms函数,即servercomms,BLEStack,timer等

I have an app that has several activities such as Main, Settings, User profile etc and I have different source for information coming into the app such as pulling data from a server, BLE comms, back ground timer etc. The plan would be to have these calling comms functions in separate classes i.e. servercomms, BLEStack, timer etc

问题是将这些类中的数据共享到活动UI的最佳方法是什么?似乎有不同的方法,例如Intent,Async,Runable,但不确定应使用哪种方法?当我看一看时,似乎会造成性能问题,如果设备内存不足,它将停止运行.

The question what is the best way to share the data from these classes to the activity UI? There seems to be different methods such as Intent, Async, Runable but not sure which one should be used? When I look at one it seems create performance issues and will stop running if the device is short on memory.

例如,如果在后台运行一个Timer,它将更新图片,Main Activity上的文本,如何从Timer类中调用函数或更新UI?我以为可以在MainActvity代码中运行一个函数,但是不确定该函数是否正确,因为我阅读它可能会影响移动应用程序的性能?

For example if there is a Timer running in the background, which will update a picture, text on the Main Activity how can I call a function or update the UI from the timer class? I thought run a function in the MainActvity code, but not sure if that is correct as I read it might affect the mobile app performance?

对不起,我知道这个问题已经得到回答,我已经关注了每个线程,但是无法找到最佳方法来使它们都可以与数据共享和更新活动UI一起工作.

Sorry, I know this question has been answered and I have followed each thread but just cannot work out the best way to make it all work with sharing data and updating the activity UI.

推荐答案

我建议您看看 EventBus 根据其说明:

I'd suggest having look at EventBus as per their description:

用于Android和Java的事件总线,简化了活动,片段,线程,服务等之间的通信.代码更少,质量更高.

OR

请查看android官方文档与片段进行通讯,其中显示了如何使用 ViewModel s在活动和片段之间以及片段与片段之间共享数据.

Have a look at the official android documentation Communicating with fragments which shows how to use ViewModels to share data between activities and fragments and fragment to fragment.

这篇关于android Java多个类更新活动UI(如何共享数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆