Android Question Close B4XDialog(custom class)

Sergey_New

Well-Known Member
Licensed User
Longtime User
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.
 

Attachments

  • MyDialog.zip
    8 KB · Views: 70
Solution
In MyDlg make Dialog Public

B4X:
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

William Lancee

Well-Known Member
Licensed User
Longtime User
In MyDlg make Dialog Public

B4X:
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
 
Upvote 0
Solution

Sergey_New

Well-Known Member
Licensed User
Longtime User
William Lancy, thank you very much!
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
n Main handle Back Key and consume event.
how to close the dialog with the back button of the device without closing the calling Activity?
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
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Mahres, thank you! I started with this, but did not guess to install Dialog Public (
 
Upvote 0
Top