简体   繁体   English

Kotlin 上的 Android:如何将数据从活动传递到事件侦听器?

[英]Android on Kotlin: How to pass data from an Activity to an event listener?

(Android newbie learning Kotlin here...) I have an app with 3 components: A UI activity where a user indicates what color car she wants to see; (Android 新手在这里学习 Kotlin ......)我有一个包含 3 个组件的应用程序:一个 UI 活动,用户指示她想看什么颜色的汽车; A service that uses the device's camera to observe a road and report every car that passes by;使用设备的摄像头观察道路并报告每辆经过的汽车的服务; And an event listener latched on the service.并且事件侦听器锁定了该服务。 The job of the event listener is to determine whether any car reported by the camera is of the requested color and, if so, do something about it.事件侦听器的工作是确定摄像头报告的任何汽车是否具有请求的颜色,如果是,则对其进行处理。

I'm tripping over something that's probably basic: how does the listener know what color to look for?我被一些可能很基本的东西绊倒了:听众怎么知道要寻找什么颜色? I can:我可以:

  1. Have the activity (a class) write to a variable owned by the listener (an object) (hmmm...)让活动(一个类)写入监听器(一个对象)拥有的变量(嗯......)
  2. Have the activity drop the color in an event bus (does it have to be so complicated?)让活动在事件总线中掉色(必须如此复杂吗?)
  3. Have the listener reach over and read a screen variable (hmmm...)让听众伸手去读一个屏幕变量(嗯……)
  4. Use some global variable in some singleton (hmmm...)在一些 singleton 中使用一些全局变量(嗯......)

What's the proper way to do this?这样做的正确方法是什么? Many thanks!非常感谢!

I think you can take help of the Shared preferences to store the value(ie color)or you can do as you mentioned in the 4th point.我认为您可以借助共享首选项来存储值(即颜色),或者您可以按照您在第 4 点中提到的方式进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从 Activity 传递到 Fragment KOTLIN? - How to pass data from Activity to Fragment KOTLIN? 如何在android Kotlin中将对象从一个活动传递到另一个活动? - how to pass object from one activity to another activity in android Kotlin? android - 如何将侦听器从片段传递给适配器? (科特林) - android - How to pass listener to an Adapter from a Fragment? (Kotlin) 如何将字符串数组从函数传递到android kotlin中的活动? - How to pass a string-array from the function to the activity in android kotlin? 带有 Kotlin 的 Android - 将数据传回上一个 Activity - Android with Kotlin - pass data back to previous Activity 如何在 Kotlin 中将数据从服务传递到活动? - How do I pass data from Service to Activity in Kotlin? 如何在 Kotlin 中将数据从主要活动传递到 BroadcastReciever - How to pass data from main activity to BroadcastReciever in Kotlin 如何将数据从活动传递到另一个活动,然后再传递到Android上的另一个活动? - How to pass data from an activity to another activity and then to another activity on Android? 将数据从Kotlin活动传递到Java DialogFragment - Pass data from kotlin activity to java DialogFragment 使用 kotlin 将数据从活动传递到片段 - Pass data from activity to fragment with kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM