I would like to advise you to consider Erel's advice to develop new projects in cross-platform B4XPages and use as many B4X components as possible to have the least impact from Android quirks.
My advice is to use B4XMainPage only for program initialization and shared variables. Although the command B4XPages.ShowPageAndRemovePreviousPages does not produce an error message, it looks like that the B4XMainPage remains in memory and is only invisible for the user, maybe because of the mentioned shared variables. I avoid using the B4XMainPage for programming routines after running into a hard-to-spot problem when refreshing page views.
If you use B4XPages you can call a subroutine when closing the B4XPage. Here you can either close the (sub)B4XPage, or quit the program. Regardless of whether this works or not, it eliminates the need to use your Java object routine.
Another possibility is to ask the user whether all data should be thrown away or stored in a database. (file). Because a B4XPages is a separate class file, you can reuse it in other projects by importing this class file into the new project. This makes reusability and software maintenance easier.
Also note that B4XPages makes it easier for you to detect if your user completes an input activity without the change being written to the database.
I also wonder if you are not overlooking the difference between testing whether a class and a database are both initialized. You call an initialization routine and with that the class or module is initialized and therefore True. This is separate from initializing a database. To avoid this misunderstanding, you can better make a global variable in the initialization routine of the database routine and makes it True when a routine is called successfully (without errors is processed), which is set to False by default.
You have the ability to handle the following specific situations in B4XPages with the routines below
' ---This event will be called once, before the page becomes visible.
' --- Called once when the page Is created. This will happen before the page
' --- becomes visible Or after a call To B4XPages.AddPageAndCreate.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
End Sub
'--- Called whenever the page becomes visible.
Private Sub B4XPage_Appear
End Sub
'--- Called whenever a visible page disappears.
Private Sub B4XPage_Disappear
End Sub
' --- Called when the app Is moved To the background. This event will be
' --- raised in all pages that implement this sub, not just the top event. This is a good place to save
' --- anything that needs To be save As the process might be killed later. Note that in B4J it Is raised when
' --- the last page Is closed.
Private Sub B4XPage_Background
End Sub
' --- Called when the app moved To the foreground.
Private Sub B4XPage_Foreground
End Sub
' --- (B4J / B4i) - Called when the page Is resized.
#IF B4J
Private Sub B4XPage_Resize
' --- Only used in B4J
End Sub
#End If
#IF B4I
Private Sub B4XPage_Resize
' --- Only used in B4I
End Sub
#End If
' --- (B4J / B4A) - In B4A it Is called when the user clicks on the back key Or
' --- on the up indicator. In B4J it is called when the user clicks on the close button
#IF B4J
Private Sub B4XPage_CloseRequest
' --- Only used in B4J
End Sub
#End If
#IF B4A
Private Sub B4XPage_CloseRequest
End Sub
#End If
' --- on the up indicator. 'In B4J it Is called when the user clicks on the close button.
' --- Called when a menu item Or BarButton in B4i Is clicked.
#IF B4J
Private Sub B4XPage_MenuClick
' --- Only used in B4J
End Sub
#End If
#IF b4I
Private Sub B4XPage_MenuClick
' --- Only used in B4I
End Sub
#End If
' --- (B4i) - Called when the keyboard state changes.
#IF B4I
Private Sub B4XPage_KeyboardStateChanged
' --- Only used in B4I
End Sub
#End If
' --- (B4J) - Called when a page Is minimized Or restored.
Private Sub B4XPage_IconifiedChanged
End Sub
' --- (B4A) - Raised after a call To rp.CheckAndRequest.
' --- rp = RuntimePermissions, the B4A RuntimePermissions library.
#IF B4A
Private Sub B4XPage_PermissionResult
' --- Only used in B4A
End Sub
#End If
#IF B4J
Private Sub B4XPage_Resize
' --- Only used in B4J
End Sub
#End If
#IF B4I
Private Sub B4XPage_Resize
' --- Only used in B4I
End Sub
#End If