Android Question Focus, Backspace and Hard/Soft Keys

RichardN

Well-Known Member
Licensed User
Longtime User
I have discovered a problem (maybe I should have known it) in using the hard 'backspace' key to switch between panels.

A simple app has two full-screen panels... 'pnlMain' and 'pnlResults', the latter is hidden and made visible to show the results of a calculation.... and then hidden again by:-

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean      'Return True to consume the event
  
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If pnlResults.Visible = True Then
            pnlResults.Visible = False
            Return True
        End If
    End If

    'We got here because only the main panel was
    'visible and the user hit backspace

    'There then follows some more code here to show an interstitial
    'ad on user requested exit

End Sub

Unfortunately the backspace key on the soft keyboard returns the same KeyCodes.KEYCODE_BACK as the hard key. Which means every time the user hits the soft backspace whist editing an EditText on the main panel the interstitial ad is shown and the program exits.

I think I am missing something simple here. How do I differently handle the user keystroke whilst editing text with focus on the main panel, while retaining the hard-key functionality for program exit???
 

RichardN

Well-Known Member
Licensed User
Longtime User
The B4A documentation states that keystrokes will be consumed by the EditText when it has the focus. In the example attached the hard backspace key fires the Activity_KeyPress event as expected returning Keycode 4. However in the same situation when the soft backspace key is pressed it deletes an EditText character as expected but then ALSO fires the Activity_KeyPress event (returning 67) which was rather unexpected.

As far as I have seen the soft backspace key is the ONLY key on the soft keypads that fires Activity_KeyPress when an EditText has the focus. Surely this cannot be intentional behaviour ?


Test Platform: Samsung GT-I9505 Android 4.4.2
 

Attachments

  • Focus_Test.zip
    7.6 KB · Views: 308
Last edited:
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Yes exactly, the number is deleted in the EditText, the Activity_Keypress event is fired and the ToastMessage shows keycode 67.

I will test on other platforms when I get a chance.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
I have four test devices:

HTC Desire Android Version 2.2.2
Samsung Galaxy Tab Android Version 4.4.2
Samsung S4 Android 4.4.2
Samsung S5 Android 4.4.2

Using the simple test app above the first two devices perform as expected. The last two fire the Activity_Keypress event when the backspace key (67) is pressed when the EditText has focus, this should not happen. This caused a lot of head-scratching over unplanned Activity_Keypress activity.

.....Go Figure!
 
Upvote 0
Top