Android Question [Solved] How to handle stuff before closing activity

Pflichtfeld

Active Member
Licensed User
I have a (child)-activity, which user can close clicking a label (lblback) or with Back-key. If user has edited on of the textfields on this activity these changes must be saved.
I tried it this way:
B4X:
Sub LblBack_Click
    If hasChanges Then AskForSave
    Activity.Finish
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event, also cancelt den Schließen-Vorgang
    If KeyCode = KeyCodes.KEYCODE_BACK And hasChanges Then
       AskForSave
   End If
   Return False
End Sub

private Sub AskForSave
    Msgbox2Async("Es gibt ungesicherte Änderungen. Sollen die gespeichert werden?", "Bitte wählen!", "Ja", "", "Nein", Null, False)
    Wait For Msgbox_Result (Result As Int)    'after this line, sub is left
    If Result = DialogResponse.POSITIVE Then
        BtnSave_Click
    End If
    hasChanges=False
End Sub

But the code seems to return from sub AskForSave, Line "Wait For Msgbox_result.. " to the calling sub and activity is finished without saving.
What must I change for this issue to get it to work properly? Must I use the non-async msgbox here?
 

Pflichtfeld

Active Member
Licensed User
Try with return = true (to consume the event). The "wait for" continues the code execution! Then call your parent activity by code from AskForSave.
Thank you KMatle! I first didn't understand you fully. But now I got it to work:
B4X:
Sub LblBack_Click
    AskForSave
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event, also cancelt den Schließen-Vorgang
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        AskForSave
    End If
    Return True
End Sub

private Sub AskForSave
    If hasChanges Then
        Msgbox2Async("Es gibt ungesicherte Änderungen. Sollen die gespeichert werden?", "Bitte wählen!", "Ja", "", "Nein", Null, False)
        Wait For Msgbox_Result (Result As Int)
        If Result = DialogResponse.POSITIVE Then
            BtnSave_Click
        End If
    End If
    hasChanges=False
    NewGrinder=0
    Activity.Finish
End Sub
 
Last edited:
Upvote 0
Top