Android Question Foreground App

Christian321

Member
Licensed User
Longtime User
Dear All,

willing to program a software for my little daugther I am facing the following problem: My kid always manages to send my app to the background. She does not know what she is doing, thus presses all buttons including 'back' and 'home'. Aware of the fact that this drains the battery I want my B4A application to stay upfront no matter what happens.

Therefore I tried two ways:

(1) In the Main activity:

Sub Activity_Pause (UserClosed As Boolean)
StartActivity(Me)
End Sub



(2) In the Starter service:

Sub Process_Globals
Dim myWatchdogTimer As Timer
Dim KeepServiceRunning As Boolean = True
End Sub


Sub Service_Create
myWatchdogTimer.Initialize("CheckMainForeground", 300)
myWatchdogTimer.Enabled = True
End Sub

Sub CheckMainForeground_Tick
If KeepServiceRunning = True And IsPaused(Main) Then StartActivity(Main)
End Sub



Both ways work well with the back button, however when I press the home button it takes seconds to bring back my application to foreground. Enough time for my kid to do other weird thing to my phone.

I am sure there is a better way to force the activity to stay. By any chance, maybe without having to restart the main activity each time? And yes, it would be ideal if the screen stayed always-on...

Many thanks for your help in advance,
Christian
 

panagiotisden2

Active Member
Licensed User
Longtime User
also you can use this in the main activity so the app wont exit when the back button is pressed
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then   
    'do nothing
    End If
   
End Sub
 
Upvote 0

Christian321

Member
Licensed User
Longtime User
try to remove the
B4X:
And IsPaused(Main)
in the timer code. Maybe there is a delay to show the activity as paused

I did try. Funny story, indeed my app stays in front now, but no events will be handled at all any more. Including my Rescue-"End"-Button.
Now the app is too sticky, even itself cannot finish. :p
 
Upvote 0

Christian321

Member
Licensed User
Longtime User
also you can use this in the main activity so the app wont exit when the back button is pressed
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then  
    'do nothing
    End If
  
End Sub

I tried this, too. No change. However, the back-button isn't big a problem. Pressing back the Activity_Pause --> StartActivity(Main) works well. It is the home-button that gives me the trouble. But many, many thanks for your thoughts anway!
 
Upvote 0

panagiotisden2

Active Member
Licensed User
Longtime User
I did try. Funny story, indeed my app stays in front now, but no events will be handled at all any more. Including my Rescue-"End"-Button.
Now the app is too sticky, even itself cannot finish. :p

isn't this what you want? even you cant stop it, your daughter wont even have a chance :p

in main activty add a button
B4X:
sub button1_click
callsub(starter,"stopwatchdogs")
end sub

and in the starter
B4X:
sub stopwatchdogs
stopservice(me)
end sub

and make the timer's interval 1000ms(1 sec)

note that the service might lead to a battery drain
 
Upvote 0

Christian321

Member
Licensed User
Longtime User
Too bad and I feel really sorry, but still, as long as I use
CheckMainForeground_Tick --> If KeepServiceRunning = True Then StartActivity(Main)
all my events won't be handled any more, including "_click". Even with a timer interval of 1s:
myWatchdogTimer.Initialize("CheckMainForeground", 1000)

And I tried hard to end this:
Sub Button1_Click
CallSub(Starter,"stopwatchdogs")
StopService(Starter)
ExitApplication
End Sub


I suppose, starting the activity repetetively in short intervals uses too many ressources.
And I start having the feeling that this brutal method isn't the right way, anyway.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Did you try the "kiosk mode" set up?
I never used it but it sounds promising...

udg
 
Upvote 0

Christian321

Member
Licensed User
Longtime User
Hi udg,

I tried the kiosk mode example. In fact, I see the same behavior similar to my approach.
In my case even just putting

B4X:
Sub Activity_Pause (UserClosed As Boolean)
    StartActivity(Me)
End Sub

shows the exact same behavior. But many thanks anyway.

Update: The KioskAppExtended does the job. Very complex solution but working. Thanks for the hint...
 
Last edited:
Upvote 0
Top