Android Question Avoid the app to exit when swiped from right to left

Mattiaf

Active Member
Licensed User
Hi, on my phone I don't have the bar in the bottom with the back button, home etc. I remove it and I'm just using the swipe gestures cause i find it easier to use it.
So in order to go back, for any app, I swipe from the left side to the center of the screen.
This makes the app to exit. I found online some solutions but they only works for the back button, and in this case i'm not pressing any back button.
Is there a way to fix it?
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
  Select KeyCode
  Case KeyCodes.KEYCODE_BACK
      
       Return true
  End Select
End Sub

this code obviously doesn't work
 

Mattiaf

Active Member
Licensed User
Add Log(KeyCode) to this sub. Do you see any message in the logs?
If you mean something like this
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Select KeyCode
        Case KeyCodes.KEYCODE_BACK
             Log(KeyCode)
            Return True
    End Select
End Sub

then no log appear
 
Upvote 0

Brian Dean

Active Member
Licensed User
Longtime User
You are logging the keycode in the wrong place. What Erel means is ...
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Log(Keycode)                    ' <=== Here is where you see the keycode
    Select KeyCode
        Case KeyCodes.KEYCODE_BACK
            Return True
    End Select
End Sub
 
Upvote 0

teddybear

Active Member
Licensed User
Hi, on my phone I don't have the bar in the bottom with the back button, home etc. I remove it and I'm just using the swipe gestures cause i find it easier to use it.
So in order to go back, for any app, I swipe from the left side to the center of the screen.
This makes the app to exit. I found online some solutions but they only works for the back button, and in this case i'm not pressing any back button.
Is there a way to fix it?
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
  Select KeyCode
  Case KeyCodes.KEYCODE_BACK
    
       Return true
  End Select
End Sub

this code obviously doesn't work
I don't think you can get any KeyCode, cause you did not press any key, just touched screen. you should check what you do in event swiped
 
Last edited:
Upvote 0

Mattiaf

Active Member
Licensed User
You are logging the keycode in the wrong place. What Erel means is ...
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Log(Keycode)                    ' <=== Here is where you see the keycode
    Select KeyCode
        Case KeyCodes.KEYCODE_BACK
            Return True
    End Select
End Sub
no log again. @teddybear is right, I am not pressing any keycode, i'm just touching the screen. @Erel do you think is there a work around? Thanks
 
Upvote 0

teddybear

Active Member
Licensed User
You should find out the reason that cause the app exit, is it crashed or activity_pause? do you have a touch event?
 
Upvote 0

Mattiaf

Active Member
Licensed User
I guess that this swipe is equivalent to the "recent apps" button and not the back key. This means that it cannot be intercepted.
I might be wrong but this swipe is not equivalent to the recent apps, it just works as back button.. At least, that does that for me on my galaxy s20+
 
Upvote 0

Mattiaf

Active Member
Licensed User
The back key raises this event. This is the way to intercept it.
It is unlikely to work but see whether the B4XPage_CloseRequest event is raised when you swipe.
Have you ever personally tried it using the swipe gesture?
this code doesn't show any log..
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Log(Keycode)                  
    Select KeyCode
        Case KeyCodes.KEYCODE_BACK
            Return True
    End Select
End Sub

but trying with
B4X:
Private Sub B4XPage_CloseRequest As ResumableSub
    Log("closing")
End Sub
i get
B4X:
Logger connected to:  samsung SM-G985F
--------- beginning of main
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
closed
Error occurred on line: 476 (B4XPagesManager)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
    at Chiavii.ronda.b4xpagesmanager$ResumableSub_HandleCloseRequest.resume(b4xpagesmanager.java:858)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1772)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8741)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

any idea?
 
Upvote 0
Top