Just now I make a library to exclude inline java statements in my app.
In updated test.zip you can find library (jar, xml) and Basic test application.
During experiments I found that StartActivity (Main) in my sample does not raise Activity_Create event. Activity_Resume only.
This means that StartActivity (Main) was enough from the beginning (I tested in API 26 & 27 emulator).
After experiments with Starter service I decided to check ordinary service. Works fine also.
Can you make a sample, where StartActivity (Main) is not enough ?
I reconstructed a sample and tested under API 26/27
Run sample and switch to another app. After 10 seconds your app appears again.
Works fine without any "clever" functions.
I compiled in B4A 8.30 (this is important). Do you use this release ?
But I think, that sometimes is this service killed when I put App to PAUSE mode and
B4X:
If IsPaused(Main)=True Then StartActivity (Main)
is not functioned.
SEMEN - thank you for your help, I am trying your each uploaded project, but I think, that sometimes it functioned perfect, sometimes it is no functioned.
The last project is without your library. Its true? This last example is very easy and I think that useful in simply Apps. I dont know if I have true.
I am still trying and finding best solution,
thank you
p4ppc
if you have B4A 8.30 and talk about last variant, your answer sounds strange for me.
Did you test in emulators or in native phones (with custom firmware) ?
SEMEN, yes I have 8.30...I am testing it on the phone and tablet with A8.
As I wrote - it is possible, that I am doing something wrong, but I am trying to do everything right....Maybe I need to do more tests
But - thank you very much for your help, you give me the direction - how to do tests. Situation with A8 is really different than with earlier version
I made experiments on my enough fast smartphone OnePlus 5T (A5010).
In general behaviour was the same as in emulators until I began to start many applications. Happens the same what you wrote - sometimes my example does not restore window. I looked Log. Service starts activity, but there is no Activity_Resume event. More ... I see attempt to close Starter service
I see the same. After I push round button on my phone first time, the app is refreshed, second time - the app is refreshed, third time - app is staying in PAUSE mode.
If I push the rectangle button on my phone, app is refreshed each time. I put here picture - to attachement - only for imagination, what is round button and rectangle button. So the trouble is that app is staying in PAUSE mode.
Services are not a serious problem. As I understand foreground services work in Oreo exactly like in Android 7-.
I made simple test. Activity Main:
B4X:
Sub Process_Globals
Public booleanIsRunning As Boolean
End Sub
Sub Globals
End Sub
Sub Activity_Create (FirstTime As Boolean)
Activity.Color = Colors.RGB (Rnd (0, 255), Rnd (0, 255), Rnd (0, 255))
End Sub
Sub Activity_Resume
booleanIsRunning = True
End Sub
Sub Activity_Pause (UserClosed As Boolean)
booleanIsRunning = False
StartService (Secondary)
End Sub
and service Secondary:
B4X:
Sub Process_Globals
Private objectTimer As Timer
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS ' Theoretically, not needed
End Sub
Sub Service_Start (StartingIntent As Intent)
objectTimer.Initialize ("timer", 10000)
objectTimer.Enabled = True
Log ("Wait 10 seconds")
End Sub
Sub timer_Tick
objectTimer.Enabled = False
Log ("StartActivity")
StartActivity (Main)
Do While (Main.booleanIsRunning = False)
Sleep (100)
Loop
StopService (Me)
End Sub
After some time the service (which is foreground) starts Activity, but Android does not activate an activity. Why ?
Sub Process_Globals
End Sub
Sub Activity_Create (FirstTime As Boolean)
Activity.Color = Colors.RGB (Rnd (0, 255), Rnd (0, 255), Rnd (0, 255))
End Sub
Sub Activity_Pause (UserClosed As Boolean)
StartServiceAt (Secondary, DateTime.Now + .25 * 60 * 1000, True)
End Sub
Service Secondary
B4X:
Sub Process_Globals
End Sub
Sub Service_Start (StartingIntent As Intent)
StartActivity (Main)
StopService (Me)
End Sub
In both cases it's neccessary to do a lot of switches until the program will die.
It looks that Android calculates summary time in background and compares with the limit.
When limit is over, Android holds the app per some time and gives new limit.
So, sooner or later an incindent will occur.
My summary - to minimize time of background processes as possible.
In Activity_Pause (before StartServiceAt) stop other services by yourself.
If not possible, make services foreground (Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS)
SEMEN - I am at the end. With your example I have this results: if I push round button in Android device then first or senod or third time is App refreshed and then nothing. As you describe in your messages. I dont know what can I do...
Please have somebody some example - how to refresh app from service correctly when the app is in PAUSE mode?
On my phone the problem with my sample occurs after a lot of restarts.
My latest test.zip based on Erel's advice about StartServiceAt and foreground service.
Anyway, it's not a solution, because there is no garantee that activity will be started when we want.
It's not absolutelly clear how Android calculates time in background and holds processes. But it's possible to expect that Android binds background time to PID (Process ID). Probably, time to foolish Android and to restart itself with new PID, using pending intent.
Approximatelly so (the code from stackoverflow, easy to transform
I think, that I must be grateful to you, because you are spending with me lot of time thank you very much.
I will try your theme. I think before Android 8 it was everything more clearer. I go to try more experiments