Android Question check if my app is active

peacemaker

Expert
Licensed User
Longtime User
HI, All

Before in small apps i was checking the app status (for toast notifications) such way:
B4X:
Sub InAppToastOnly(Text As String, LongToast As Boolean)
If IsPaused(Main) = False OR IsPaused(Logs) = False OR IsPaused(About) = False Then
    ToastMessageShow(Text, LongToast)
End If
End Sub

So, i have to list all the activities manually.
Notification for the user should be shown only if he is working in my app only.

But now in huge project with tens of activities... maybe any smart variant is possible ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add this code to a code module or the starter service:
B4X:
Sub Process_Globals
   Private Activities As List
End Sub

Sub Service_Create
   Activities = Array(Main, <any other activities in your project>)
End Sub

Public Sub IsAnyActivityVisible As Boolean
   For Each act As Object In Activities
       If IsPaused(act) = False Then Return True
   Next
   Return False
End Sub

It will be more convenient to add it to a code module and call an 'init' sub from Service_Create of the starter service. The list will be set in that sub.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks, @Erel. So - no any "smart" way, manual iteration anyway :)
No any way to save the module names in your internal B4X variables ?
To be possible to use activity names in the code...
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
No any way to save the module names in your internal B4X variables ?
Actually there is another way:

B4X:
Sub IsAnyActivityVisible As Boolean
   Dim jo As JavaObject
   Return jo.InitializeStatic("anywheresoftware.b4a.BA").RunMethod("isAnyActivityVisible", Null)
End Sub
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
WOW, thanks ! Super, will test.
It deserves to make a new internal B4X function InAppToast:

B4X:
Sub IsAnyActivityVisible As Boolean
    Dim jo As JavaObject
    Return jo.InitializeStatic("anywheresoftware.b4a.BA").RunMethod("isAnyActivityVisible", Null)
End Sub

Sub InAppToast(Text As String, LongToast As Boolean)
    If IsAnyActivityVisible Then
        ToastMessageShow(Text, LongToast)
    End If
End Sub

And actually - most of toast in any app - should be exact this, as it's annoying, if any toast is from hidden background apps. Regular toast is only for a Foreground service.
 
Upvote 0
Top