Android Question Service switching updates between two Activities

JohnK

Active Member
Licensed User
Longtime User
I have a long running service, which may have the user in another section of the application. I wish to show the current state of the service on the activity the user is currently in. Attached is an example project simplification of my issue.

If I start the service while in either Activity and stay within that Activity, it all works fine. However, if I switch to the other activity, the app appears to stall.

I have given the service the following logic to know which activity is currently visible:
B4X:
If Globals.ActivityIndex = 1 Then
    CallSub3(Main, "ShowText", I, P)
Else If Globals.ActivityIndex = 2 Then
    CallSub3(SecondActivity, "ShowText", I, P)
End If
Where ActivityIndex can be 1, 2 or 0 if its in the process of switching between activities.

I can not see where I am going wrong?
 

Attachments

  • ServiceAndTwoActivities.zip
    8 KB · Views: 179

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

JohnK

Active Member
Licensed User
Longtime User
I do not see how the link you provided solves my problem. The code returns the Package Name of the currently running task, with no distinction between which activity is actually in the foreground (have not looked at MSOS Library).

What testing this code did highlight is that even when I take the code out to update the currently active Activity, simply de-activating the Activity that started the service, appears to cause issues. ie start the service in the second form and then run StartActivity(Main) causes the app to stall.

The attached project is an even more simplified one that simply has two activities from which you can start the service, and then switch to the other activity (causing the app to "stall" and the screen go black).

So this now is simply the case of starting a service from an activity and then switching to another activity.
 

Attachments

  • ServiceAndTwoActivities3.zip
    7.2 KB · Views: 221
Upvote 0
Top