Android Question Consume mqtt messages from different activities

Acuario

Member
Licensed User
Longtime User
I have an app with multiple activities that uses mqtt.
I want to connect to the mqtt broker when the app starts then from different activities consume messages from the broker.

I have a separate module with the mqtt code. In the Main activity in Process_Globals I have my MqttClient declared.
In the mqtt module I have my connect code and code for sending an mqtt message. This code works.

I have tried to add the mqtt_connected code to the mqtt module but this doesn't get run when mqtt connects. If the code is in Main then it does get run.
The same is true for the mqtt_MessageArrived.

When I open a different activity I receive log messages 'sending message to waiting queue (mqtt_messagearrived)'

What do I need to do to be able to consume the mqtt messages from different activities (the active activity)?

Thanks.
 

Acuario

Member
Licensed User
Longtime User
I have moved my code to B4XPages but still have a problem.

When my MQTT message is received it can update different pages. If one of the pages that should be updated has not yet been viewed then the program crashes as below when trying to set the text of the view (a label in this case).

B4X:
Error occurred on line: 152 (Starter)
java.lang.RuntimeException: Object should first be initialized (B4XView).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
    at anywheresoftware.b4a.objects.B4XViewWrapper.asLabelWrapper(B4XViewWrapper.java:210)
    at anywheresoftware.b4a.objects.B4XViewWrapper.setText(B4XViewWrapper.java:226)
....

Is there any way to either pre-load the pages or a way to determine the current active page?
 
Upvote 0
Top