Android Question ToastMessage resumes its own activity

alain bertrand

Member
Licensed User
Longtime User
Activity1 shows a list of items.
Activity2 displays information about the selected item.
Activity2 CallSubDelayed(Activity3) for selecting Google contacts receiving a snapshot of Activity2.
Activity3 returns to Activity2 the list of selected contact; CallSubDelayed2(Activity2,,the_list)
Activity2 sends mail(s) using Google mail service.
in Activity2 if HttpJob is Success then ToastMessageShow("Message sent successfully").
All right.
The problem; Google mail may require a ~3 second delay, if the user has KEYCODE_BACK before this delay, Activity2 (the user want to leave) pops up again.
How to avoid this behavior?
 

JohnC

Expert
Licensed User
Longtime User
Once possible way is to intercept the KeyCode_Back keypress and prevent it from doing anything if it's a bad time to close the activity:

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    'Return True to consume the event
    
    Log("Keypress: " & KeyCode)
    
    Select Case KeyCode
        
        Case KeyCodes.KEYCODE_BACK    'user pressed the back button
            If IgnoreBackKeyNow then     'if its not OK to close the activity, then ignore it
                Return True    'tell the OS to do nothing           
            End If
    End Select
    
End Sub
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
consume KEYCODE_BACK until it's safe to let it follow its normal course. you may need to set a flag so that activity2 knows when to ignore KEYCODE_BACK and when not to.

not clear what you mean by activity2 "pops up again". if user is still in activity2, what's to "pop up"?
 
Upvote 0

alain bertrand

Member
Licensed User
Longtime User
Thanks both of you.
Trapping the KeyCode_Back until HttpJob is success to stay on Activity2 (and prevent the user from shifting down to Activity1) makes sense.
As in Activity2 'Wait For (j) JobDone(j As HttpJob)' takes sometimes a lot of seconds (network? Google server?), the user will believe the application is frozen.
It's a mistake for me to accuse the only ToastMessage of bringing up Activity2 again, it's all the piece of script (in Activity2) after 'Wait...success' (Toast, clear cache...).
So my question is nonsense. Sorry.
I'd better to transfer the task of sending email from Activity2 into a service.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
users become impatient if they don't think some task is being carried out or has completed. moving your network code to a service doesn't necessarily change that perception. the service allows the main activity to continue without freezing, but the user will not know what's happening in the service. you would still, eg, have to consume the back key. you need to invoke a progress message ("sending. almost done...") or show a little message before and after sending the email to let the user know what the deal is. you see this in real life all the time: "don't press this button again", "please wait", etc).
 
Upvote 0
Top