Tell me, please, how to close the dialog with the back button of the device without closing the calling Activity? And if the dialog was not open, then close the Activity.
An example is attached.
Sub Class_Globals
Private xui As XUI
Public Dialog As B4XDialog
Private lbMess As Label
Private lbTitle As Label
Private img As ImageView
Private rs As ResumableSub
End Sub
In Main handle Back Key and consume event.
B4X:
Sub Globals
Private xui As XUI
Private dlg As MyDlg
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
Log("Back Pressed")
If dlg.Dialog.Visible Then
dlg.Dialog.Close(xui.DialogResponse_Cancel)
Return True
Else
Return False
End If
Else
Return False
End If
End Sub
Sub Class_Globals
Private xui As XUI
Public Dialog As B4XDialog
Private lbMess As Label
Private lbTitle As Label
Private img As ImageView
Private rs As ResumableSub
End Sub
In Main handle Back Key and consume event.
B4X:
Sub Globals
Private xui As XUI
Private dlg As MyDlg
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
Log("Back Pressed")
If dlg.Dialog.Visible Then
dlg.Dialog.Close(xui.DialogResponse_Cancel)
Return True
Else
Return False
End If
Else
Return False
End If
End Sub
May I suggest this as another way. Tested. it works:
1. Leave Dialog as private. Add 2 methods to your class dlg:
B4X:
Public Sub CloseDialog
Dialog.Close(xui.DialogResponse_cancel)
End Sub
public Sub VisibleDialog As Boolean
If Dialog.Visible Then
Return True
Else
Return False
End If
End Sub
2. Then in the main add this:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
If dlg.VisibleDialog Then
dlg.CloseDialog
Return True
End If
End If
Return False
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.