Android Question Back button(on Keypress event) crashing app

Pravin Shah

Member
Licensed User
Longtime User
Hello,

I have added the following code to capture back key and write my own logic.

B4X:
Sub Activity_KeyPress (KeyCode As Int)
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        HomePage.MediaPlayer1.Play
    End If
   
End Sub
But whenever I press back button the app crashes with following error log. But if I remove the activity_keypress code module then it works properly. I am not able to debug this problem, can someone help me with this? thanks

B4X:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
    at b4a.weddingapp.viewvideo$HandleKeyDelayed.runDirectly(viewvideo.java:228)
    at b4a.weddingapp.viewvideo.onKeyDown(viewvideo.java:215)
    at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
    at android.app.Activity.dispatchKeyEvent(Activity.java:2361)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1819)
    at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3575)
    at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3545)
    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2795)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
 

An Schi

Well-Known Member
Licensed User
If i remember correctly you have to use 'return true or false' properly in this case.
I'm on my phone in the train, so i can't do it for you. But you will find keycode subs examples here with the correct use of return.
 
Upvote 0

An Schi

Well-Known Member
Licensed User
Here is an old post from myself...lol

Thanks! :)
The CallSubDelayed solution is working. Below is my whole construct in case someone will dig this out in the future....


B4X:
Private KC As String

Sub Activity_KeyPress (KeyCode As Int) As Boolean
   If KeyCode = KeyCodes.KEYCODE_BACK Or KeyCode = KeyCodes.KEYCODE_MENU Then
     KC = KeyCode
     CallSubDelayed(Me, "HandleSideBar")
     Return True
   End If
   Return False
End Sub

Sub HandleSideBar
   If KC = KeyCodes.KEYCODE_MENU Then
     If PanelWithSidebar.IsSidebarVisible = True Then
       PanelWithSidebar.CloseSidebar
     Else
       PanelWithSidebar.OpenSidebar
     End If
   Else If KC = KeyCodes.KEYCODE_BACK Then
       If PanelWithSidebar.IsSidebarVisible = True Then
         PanelWithSidebar.CloseSidebar
       Else
         If WV1Extra.CanGoback = True Then
           WV1.Back
         Else
           Activity.Finish
         End If
       End If
   End If
End Sub
 
Upvote 0
Top