https://kotlinlang.org logo
#android
Title
# android
t

Tariq Ebadi

11/06/2023, 8:19 PM
Anyone know how to put together a “global listener” for android? I’m using awsIot to listen for device to device communication. When I receive a message, I want this message to be listened for all screens of the app, and then navigate to a specific screen. So, how can I use awsIotClient globally?
not kotlin but kotlin colored 2
f

Frank Bouwens

11/07/2023, 12:30 PM
not kotlin but kotlin colored But, let me give you a pointer; Sounds like what you need is an Event Bus. Using an Event Bus, you can listen (or subscribe) to a single source, from multiple locations in your app. https://square.github.io/otto/ https://dev.to/mohitrajput987/event-bus-pattern-in-android-using-kotlin-flows-la In your case; You can make a Bus (singleton) in you Application, and then subscribe in every of your screens (in onCreate). (don’t forget to unsubscribe in the onDestroy) When the AwsIotClient fires, publish this fact onto the Bus, and all the subscribers (screens) will know it happend, and can respond accordingly. The specifics will depend on your own implementation. Hope this helps! Good luck!
t

Tariq Ebadi

11/07/2023, 5:43 PM
I really appreciate the suggestion Frank. Since I’m using compose, do you know if the same subscription is possible for compose screens? My navigation is also based on compose
f

Frank Bouwens

11/07/2023, 6:57 PM
I’m not familiar enough with Compose to answer; But I would assume you should be able to do it!
t

Tariq Ebadi

11/07/2023, 10:50 PM
Are you at all familiar with device/app locking? The idea is that a device/app is registered and able to use the app. The app can become unregistered at any moment. At that moment, the app should pop to the registration screen, no matter where the user is currently. How can I make this dynamic lock screen?
f

Frank Bouwens

11/08/2023, 9:51 AM
You could have a login-activity, and a main-activity (with all your compose screens); The main-activity could listen to the eventbus, and when triggered, start the login-activity + finish itself.
t

Tariq Ebadi

11/08/2023, 5:27 PM
2 activities is just bad design. And it needs to be global authentication. User needs to get the authentication screen in all areas of the app if I disable a specific device authentication
o

Omer Atiq

12/03/2023, 7:48 PM
You should use shared flow, that will solve all your problems.
2 Views