back key press while editing

imbault

Well-Known Member
Licensed User
Longtime User
Hi,

Does anyone knows how to intercept the back key while editing an EditText view.

In order to warn the user or save the edit?

Many thanks

Patrick
 

NJDude

Expert
Licensed User
Longtime User
you will have to do something like this:
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean

    If KeyCode = KeyCodes.KEYCODE_BACK Then
            
       Selection = Msgbox2("Do you want to save your changes?", "Exit", "Yes", "", "No", Null))

...
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Thank you for the answer. I know that

But it doesn't tell you if you were editing or not, it is too generic.

But thanks
 
Upvote 0

admac231

Active Member
Licensed User
Longtime User
When you press back when editing the default behaviour is to close the keyboard. I think I understand you correctly though.

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean                     
   If KeyCode = KeyCodes.KEYCODE_BACK Then
      If Edittext1.Text <> "" Then
         Dim a As Int
         a = Msgbox2("Do you want to save?","Save","Yes","Cancel","No",Null)
         If a = DialogResponse.POSITIVE Then
            'Save
         Else If a = DialogResponse.CANCEL Then
            Return True
         End If   
      End If
      Return False
   Else
      Return False
   End If 
End Sub

This will stop the activity from closing when back is pressed and if there is text in edittext1 the msgbox will show. If the user selects:

Yes -> 'Save
No -> Activity closes
Cancel -> Nothing Happens
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Well, in that case you will have to use the TextChanged property of the EditBox and setup a flag, somethig like:
B4X:
If KeyCode = KeyCodes.KEYCODE_BACK And Flag = True Then ...

You create that Flag as Boolean in Globals and if the text changes then set it as TRUE
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
My main problem is :

Those editing fields are dynamically created, under the same same :
editp.Initialize("Editp") in order to catch a validation routine.

They could be from none to 12 edittexts.

So how to call them one by one, except when I use a Sender in my validation routines


thx
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Solved adding a global variable:

Dim GlobalSender As EditText

in focusChanged :

Sub Editp_FocusChanged (HasFocus As Boolean)

GlobalSender= Sender
end sub

then checking:

Sub Activity_KeyPress (KeyCode As Int) As Boolean
Select KeyCode
Case KeyCodes.KEYCODE_BACK
If GlobalSender.IsInitialized Then
verif_valid(GlobalSender)
End If

End Select
End Sub

Anyway, thanks to all.

Patrick
 
Upvote 0
Top