Android Question Service calling Sub in an inactive Activity

Facilauto Juan

Member
Licensed User
Longtime User
Hello there!

I have a chat Activity and a service for it.

The Activity shows the messages and sends new ones.

The service check online if there is new messages and, in that case, shows them in the Chat Activity calling a function in the activity. The httpjob execution is in the chat but the "jobdone" method is in the Activity.

My problem is... What if the service executes the httpjob but the activity isn't active when the http response arrives?

Thanks for your answers and your time.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Not exactly.

You should read about CallSubDelayed to understand how it works: Using CallSubDelayed to interact between activities and services

The summary is:
1. If any activity is currently visible then the target activity will be resumed (if it is not active) and the event will be raised.
2. If no activity is visible then the event will be queued and will be handled when the activity resumes. You will see a message about it in the logs.
 
Upvote 0

Facilauto Juan

Member
Licensed User
Longtime User
Ok, then I have a problem:

The chat is only one of the various activities I have in my app.
The chat service acts only when the chat activity is active.
The chat service launchs the httpjob call.
The chat service receive the "jobdone" response in a method.

Now let's say that the service launchs a the httpjob call and the user goes to another activity in the same app. When the http response comes, the chat will be opened again, and I don't want that.

Is there any way to cancel that response in order to avoid the unwanted "popup" of the chat activity?

Thanks for all the responses.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I think the way to do this is to separate the data from the UI.
The service should retrieve the data and write it to a file or a DB. Then use CallSub to refresh the activity, only if it is visible to give Real-time update.
Otherwise, load the data on activity resume, and refresh the UI.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
The jobDone should be placed in the service. Then, when data arrives, check if activity is active, thus send data using your sub. If not active, show a notification, then let user decide whether will read now her messages. This is the way I would handle the situation.
 
Upvote 0
Top