can't say this is the best. or even the only way.
but here's something you might try in your service. basically, it gave a list of running
processes. nowadays it only lists your app or, presumably, nothing if your app isn't
loaded. hard for me to test since i don't have a service ready to go and, mainly,
you can't restart an activity from a service anymore (if i understand correctly).
but if you query the activitymanager and you see your app, then you know it's in
memory. if you don't see it, then it's dead. anyway, that's how i read it.
note the "importance" field. this indicates the state of any processes listed. you can
also get the names of the running processes to compare against your own app's name.
as i say, nowadays you can only see your own app. if you're running the app on an
older device with your b4a v9, you might see more.
you can look up the constant values for "importance" in google's documentation for
android.app.ActivityManager.RunningAppProcessInfo
Dim jo As JavaObject
Activity.LoadLayout("Layout")
jo.Initializecontext
Dim activitymanager As JavaObject
activitymanager = jo.RunMethod("getSystemService",Array("activity"))
Dim processlist As JavaObject = activitymanager.RunMethod("getRunningAppProcesses",Null)
Log(jo.RunMethod("getPackageName",Null))
If processlist.IsInitialized Then
For Each p As JavaObject In processlist.As(List)
Log(p.GetField("importance")) ' you can look 'em up
Log(p.GetField("processName")) ' compare with getPackageName above
Next
Else
Log("failed")
End If
i ran it on android 12 + b4a 12.2. ran as expected under google's current conditions.