Stop Home button exiting Application

LisaM

Member
Licensed User
Longtime User
Hi,

Is it possible to stop my application from closing when the home button is pressed using B4A? and if so does it matter if it is a physical home button? the reason is i am building and educational app for my 2 yr old and would like to be able to stop him from accidentally exiting the app and consequently accessing other areas of my phone. I have added another way of exiting the application by long clicking on a button on the home screen which makes visible a quit button
I have had a search and found how to trap the back button from exiting but it does not work for my home button.

Also I use this code to switch between layouts using the 1 activity. Is there a better way to write this code as it seems very slow the further i progress through the layouts. Maybe using a for each loop? if so how would i write it?
I did try using the sliding panels code but could not get it to work using layouts.

this is the button event
B4X:
Dim counter As Int
Dim counterpage As Int
B4X:
Sub Button2_Click
   counterpage = counter + 1
   counteraspage
   counter = counterpage   
End Sub

Sub Button1_Click
   counterpage = counter - 1
   counteraspage
   counter = counterpage
End Sub

this sets the layout dependant on the counterpage value
B4X:
Sub counteraspage
   If counterpage = 0 Then
   Activity.LoadLayout("A")
   End If
   If counterpage = 1 Then
   Activity.LoadLayout("B")
   End If
   If counterpage = 2 Then
   Activity.LoadLayout("C")
   End If
   If counterpage = 3 Then
   Activity.LoadLayout("D")
   End If
   If counterpage = 4 Then
   Activity.LoadLayout("E")
   End If
   If counterpage = 5 Then
   Activity.LoadLayout("F")
   End If
   If counterpage = 6 Then
   Activity.LoadLayout("G")
   End If
   If counterpage = 7 Then
   Activity.LoadLayout("H")
   End If
   If counterpage = 8 Then
   Activity.LoadLayout("I")
   End If
   If counterpage = 9 Then
   Activity.LoadLayout("J")
   End If
   If counterpage = 10 Then
   Activity.LoadLayout("K")
   End If
   If counterpage = 11 Then
   Activity.LoadLayout("L")
   End If
   If counterpage = 12 Then
   Activity.LoadLayout("M")
   End If
   If counterpage = 13 Then
   Activity.LoadLayout("N")
   End If
   If counterpage = 14 Then
   Activity.LoadLayout("O")
   End If
   If counterpage = 15 Then
   Activity.LoadLayout("P")
   End If
   If counterpage = 16 Then
   Activity.LoadLayout("Q")
   End If
   If counterpage = 17 Then
   Activity.LoadLayout("R")
   End If
   If counterpage = 18 Then
   Activity.LoadLayout("S")
   End If
   If counterpage = 19 Then
   Activity.LoadLayout("T")
   End If
   If counterpage = 20 Then
   Activity.LoadLayout("U")
   End If
   If counterpage = 21 Then
   Activity.LoadLayout("V")
   End If
   If counterpage = 22 Then
   Activity.LoadLayout("W")
   End If
   If counterpage = 23 Then
   Activity.LoadLayout("X")
   End If
   If counterpage = 24 Then
   Activity.LoadLayout("Y")
   End If
   If counterpage = 25 Then
   Activity.LoadLayout("Z")
   End If
   If counterpage > 25 Then
   Activity.LoadLayout("A")
   counterpage = 0
   counter = 0
   End If
End Sub

any help appreciated thnx
 

mangojack

Expert
Licensed User
Longtime User
Here's my two cents worth .. and there might be a more efficient method. Only tested with 3 layouts . How do you stop counterpage reducing past 0 ?

B4X:
Sub Process_Globals
      
   Dim alpha As String    :alpha = "ABCDEFJHIJKLMNOPQRSTUVWXYZ"
   
End Sub

Sub Globals

   Dim Button2 As Button
   Dim Button1 As Button
   Dim counterpage As Int

End Sub
   
Sub Button1_Click
    'if counterpage = 0 then Return   'might be neccessary   
    counterpage = counterpage - 1
    GetLayout(counterpage)
End Sub

Sub Button2_Click
    counterpage = counterpage + 1
    GetLayout(counterpage)
End Sub

Sub GetLayout(nPage As Int)
    
   If nPage > 25 Then 
      counterpage = 0           
      nPage = 0               
   End If
   
   Dim  nLayout As String
   nLayout = alpha.CharAt(nPage)
    Activity.LoadLayout(nLayout)

End Sub

Cheers mj
 
Last edited:
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
You will never be able to capture the Home button. This isn't a limitation of Basic4Android, it's an intended limitation in Android (to stop misbehaving apps from taking complete control).
 
Upvote 0

LisaM

Member
Licensed User
Longtime User
Thanks for all the great reply's
I wonder how Kids Mode by Zoodles does it then cause they have prevented the home button from exiting the app. Not that it matters it was just a query.

They code you posted looks great so much easier I will test it out and it teaches me alot thank you very much.

In the meantime i just ended up using the 1 layout and just changing the label.txt and imageview.setbackgroundimage dependant on the counter no.

if the counter went past 0 i put this code in.
B4X:
   If counterpage < 0 Then
   Label1.Text = "Z"
   ImageView1.SetBackgroundImage(LoadBitmap(File.DirAssets,"Zalmoxes.png"))
   Label3.Text = "Zalmoxes"
   Label3.Top = 75%y
   counter = 25
   counterpage = 25
   End If
so it just ends up in a loop

thank you everyone here is so helpful its great :icon_clap:
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…