Android Question [Solved] KeyCodes.KEYCODE_BACK return value?

anOparator

Active Member
Licensed User
Longtime User
I click Menu icon on Main activity:
- Button1 opens Activity2(Webview1), the Back button takes me back to Main activity.
- Button 2 makes Searchview1 visible in Main activity.

The following Sub enables the Back button to do Searchview1. Visible=False and Main stays active.
B4X:
Sub Activity_KeyPress (key As Int) As Boolean
If key = KeyCodes.KEYCODE_BACK Then
If Searchview1. Visible=True Then
Searchview1.Visible=False
Panel1.Visible=True
Return True
    Else
Return False
    End If
End If
End Sub
Pressing Back again closes everything as expected.
Would someone tell me what is causing a 'Not all code paths return a value'.
I've read https://www.b4x.com/android/forum/t...ctivity-and-get-a-return-value.84804/#content and still need more to read.
Thanks
 

ilan

Expert
Licensed User
Longtime User
Would someone tell me what is causing a 'Not all code paths return a value'.

the _KeyPress Sub is not only for the Back Button. Its also for the Volume Button and for the Menu Button,...

so when you filter it and say if pressed button is the Back Button then do this but what will happen if its not the Back button??

so adding on the bottom "Return False" will clear that warning.

B4X:
Sub Activity_KeyPress (key As Int) As Boolean
If key = KeyCodes.KEYCODE_BACK Then
If Searchview1. Visible=True Then
Searchview1.Visible=False
Panel1.Visible=True
Return True
    Else
Return False
    End If
End If

Return False '<----------
End Sub

Note that the KeyPressed sub is expecting a returned value (in our case a Boolean) so that value must be returned in any possible case otherwise its a sub that returns a value without really doing it in any scenario.
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
Thanks, makes sense.
Reminds me of Timer1.Enabled = False
 
Upvote 0
Top