Android Question b4X pages intercept the back button operation

davemorris

Active Member
Licensed User
Longtime User
Hi, Guys

I am using B4Xpages and have a problem with back button operation. I am not using the backbutton built into B4Xpages, its operation (going to the previous page) does not suit my app. However, the user can still press the backbutton on the phone (below the display) and it causes my App to crash with the follow exception

--- First few lines of exception report (hopefully enough)
java.lang.ClassCastException: java.lang.String cannot be cast to anywheresoftware.b4a.BA$ResumableSub
at b4a.Ui_Tests.b4xpagesmanager$ResumableSub_HandleCloseRequest.resume(b4xpagesmanager.java:854)
at b4a.Ui_Tests.b4xpagesmanager._handlecloserequest(b4xpagesmanager.java:821)
at b4a.Ui_Tests.b4xpagesmanager._activity_keypress(b4xpagesmanager.java:576)
at b4a.Ui_Tests.b4xpagesdelegator._activity_keypress(b4xpagesdelegator.java:68)
at b4a.Ui_Tests.main._activity_keypress(main.java:437)
'.......

In the old days (Activity based) I used the
Classic back button detectio:
Sub Activity_KeyPress (KeyCode As Int) As Boolean

If KeyCode = KeyCodes.KEYCODE_BACK Then

I have tried using the following code, as advised in post https://www.b4x.com/android/forum/threads/b4xpages-handling-the-back-key.119297/ which appeared to discuss a similar problem

B4XPages_CloseRequest but that don't help.

I could handle it with the Main "Delegates" code, but again in the above post, Erel does not recommend touching template code.

Any suggestions

Dave Morris
 
Solution
Hi, Wil

Thanks for the suggestion - the page was initialized ok. However, the back button for all other pages worked without problems. I was about to try to remove code from the page causing the problem until it was fixed. However, I decided to uninstall the App (the debug version) - There appeared to be two versions of the App (but I am not 100% sure), I also uninstalled several other related Apps.

Anyway recompiling the App for debug appeared to fix the problem - the back button operation was correct. In the past I have had strange problems with B4i Apps where the only fix was to uninstall the debug version of the software. So it now appears B4A could be suffering from the same type of problem.

So my solution (if you are running in...

jahswant

Well-Known Member
Licensed User
Longtime User
B4X:
'Return True to close, False to cancel
Private Sub B4XPage_CloseRequest As ResumableSub
    Return True
End Sub
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi jahswant

Thanks for the quick response, but still not working.

This suggestion was in the post I referred to, but it still throws an exception (slightly difference to the previous exception without B4XPages_CloseRequest())

First few lines of exception
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.keywords.Common.CallSubDebug(Common.java:1053)
at b4a.Ui_Tests.b4xpagesmanager$ResumableSub_HandleCloseRequest.resume(b4xpagesmanager.java:854)
at b4a.Ui_Tests.b4xpagesmanager._handlecloserequest(b4xpagesmanager.java:821)
at b4a.Ui_Tests.b4xpagesmanager._activity_keypress(b4xpagesmanager.java:576)
at b4a.Ui_Tests.b4xpagesdelegator._activity_keypress(b4xpagesdelegator.java:68)
at b4a.Ui_Tests.main._activity_keypress(main.java:437)

For the record, this is the code
Back button handling:
' Cancel back button
Private Sub B4XPage_CloseRequest As ResumableSub
    Return False
End Sub

Dave
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi, Thanks for the help so far, but.....

Not sure this is going to provide a solution - the Delegate code in Main has line

Return B4XPages.Delegate.Activity_KeyPress(KeyCode)

And appears this bit of code, in the template, is causing my app to crash (with the exception report in my first post).

Also changing the code in the template, apart from not recommended, could effect all the other pages and for some pages the normal back button operation is acceptable.

Dave
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi - Its the standard template version B4A-1.01 - I have not made any changes to it.

But posted as requested.
Main code:
#Region  Project Attributes
    #ApplicationLabel: User Interface Tests
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: false
#End Region

'#BridgeLogger: True

Sub Process_Globals
    Public ActionBarHomeClicked As Boolean
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
End Sub

'Template version: B4A-1.01
#Region Delegates

Sub Activity_ActionBarHomeClick
    ActionBarHomeClicked = True
    B4XPages.Delegate.Activity_ActionBarHomeClick
    ActionBarHomeClicked = False
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub

Sub Activity_Resume
    B4XPages.Delegate.Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    B4XPages.Delegate.Activity_Pause
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    B4XPages.Delegate.Activity_PermissionResult(Permission, Result)
End Sub

Sub Create_Menu (Menu As Object)
    B4XPages.Delegate.Create_Menu(Menu)
End Sub

#if Java
public boolean _onCreateOptionsMenu(android.view.Menu menu) {
     processBA.raiseEvent(null, "create_menu", menu);
     return true;
    
}
#End If
#End Region

'Program code should go into B4XMainPage and other pages.


Thanks again for the help so far.

Dave
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Does B4XPage_CloseRequest work at all for you. If not, you may have forgotten to initialize the page instance.
I can create the effect you want with your code in a small program, without any problems.
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi, Wil

Thanks for the suggestion - the page was initialized ok. However, the back button for all other pages worked without problems. I was about to try to remove code from the page causing the problem until it was fixed. However, I decided to uninstall the App (the debug version) - There appeared to be two versions of the App (but I am not 100% sure), I also uninstalled several other related Apps.

Anyway recompiling the App for debug appeared to fix the problem - the back button operation was correct. In the past I have had strange problems with B4i Apps where the only fix was to uninstall the debug version of the software. So it now appears B4A could be suffering from the same type of problem.

So my solution (if you are running in debug) is to uninstall the App, which will then re-install after compiling - and hopefully problem fixed.

But back to the original question - placing code in B4XPage_CloseRequest() now works.

Thanks for all the help from everyone, very much appreciated.

Dave
 
Upvote 0
Solution
Top