Android Question Close App message

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using the following code when someone presses the back button on the device, and it displays the message box asking if I am sure if I want to close the app, but then disappears pretty straight away even without pressing the 'yes' button.

Is there a different way in doing this?

B4X:
Sub appclose
    Dim Answ AsInt
    Dim Txt AsString

    Txt = "Do you really want to close the app ?"
    Answ=Msgbox2(Txt,"ATTENTION","Yes","","No",Null)

    If Answ=DialogResponse.POSITIVE Then
        Activity.finish 
        ExitApplication
       Else
         Return True
    End If
End Sub

Sub Activity_Keypress (KeyCode As Int) As Boolean

    If KeyCode=KeyCodes.KEYCODE_BACK Then
       appclose
    End If
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        CallSubDelayed(Me, "HandleBackKey")
        Return True
    End If
    Return False
End Sub


Sub HandleBackKey
    Dim Answ As Int 
    Dim Txt As String
    Txt = "Do you really want to close the app ?"
    Answ=Msgbox2(Txt,"ATTENTION","Yes","","No",Null)
    If Answ=DialogResponse.POSITIVE Then
         CloseApp
    End If
End Sub

Sub CloseApp
    ' Close DB
    ' Close KeyValueStore
    ExitApplication
End Sub
 
Last edited:
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
With your code it now displays the message box but when I click 'yes' it just hides the message box but the app stays open.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Ok, this seems to work up to Android 4.4 then it stops working on Android L.

Maybe a bug in Android L or Android L now does things differently.

I have added some logs in the code to see what happens and here are the results:

B4X:
Sub appclose
    Dim Answ AsInt
    Dim Txt AsString

    Txt = "Do you really want to close the app ?"
    Answ=Msgbox2(Txt,"ATTENTION","Yes","","No",Null)
    Log(Answ)   '<< This logs: -3  even when the message box is showing without selecting a item
    If Answ=DialogResponse.POSITIVE Then
        Log("Exit App")  ' This part of the code never gets triggered
        Activity.finish
        Return False
       Else
         Return True
    End If
End Sub

Sub Activity_Keypress (KeyCode As Int) As Boolean

    If KeyCode=KeyCodes.KEYCODE_BACK Then
       appclose
    End If
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'm unable to reproduce it. I tried with this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   
End Sub


Sub appclose As Boolean
  Dim Answ As Int
  Dim Txt As String

  Txt = "Do you really want to close the app ?"
  Answ=Msgbox2(Txt,"ATTENTION","Yes","","No",Null)
  Log(Answ)  '<< This logs: -3  even when the message box is showing without selecting a item
  If Answ=DialogResponse.POSITIVE Then
  Log("Exit App")  ' This part of the code never gets triggered
  Return False
  Else
  Return True
  End If
End Sub

Sub Activity_Keypress (KeyCode As Int) As Boolean
  If KeyCode=KeyCodes.KEYCODE_BACK Then
  Return appclose
  End If
   Return True
End Sub
Tested on Nexus 5 running Android L.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Erel,
Can you run the code attached on your Nexus 5 running Android L and let me know what happens. (it's basically the code you posted above)

On my Nexus 5 running Android L this is what happens..
Load app, and runs fine
Press the back button and a message comes up saying do I want to close the app with a Yes, No buttons
I press No, and the app stays open. (which is fine)
I press the back key again
Press the back button and a message comes up saying do I want to close the app with a Yes, No buttons
I press Yes this time
App still says open, doesn't close. (app should of closed, but doesn't)

Everytime I press the back key it logs -3 in the IDE log.

Are you able to upload a sample project that is working on your device so I can test it on mine since I can't get it to work?
 

Attachments

  • demo.zip
    6.3 KB · Views: 162
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

Whops..
I was using B4A v3.80.

I had downloaded it but forgot to install it.

Since updating to B4A 3.82 it worked first go.
Thanks heaps for your help.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…