Android Question HttpJob, Activity_Pause and Main

Marcus Araujo

Member
Licensed User
Longtime User
Hello,

I'm having an issue with HttpJob that I believe is pausing my activity (scSignup)... but for some reason is calling back my Main activity prior to calling the activity it was "called from" (the HttpJob is actually in a Service module).

There is no Activity.Finish or similar code that could quit the current activity.

Any ideas on how to avoid that behavior? I don't want Main activity to be called!

I already tried:
- Moving the HttpJob to a class
- Moving the HttpJob to each activity
- Moving the HttpJob to Starter service
- Using HttpRequest and HttpClient instead of HttpJob

All of the above reproduces the issue.


Below is the debugging flow:
Current activity = scSignup
* user clicks on Send signup *
Call Service's Sub: Send Data
Call scSignup's Sub: Show Waiting Dialog
* Waiting Dialog is shown very briefly *
* Service Sub Send Data steps in - Http.PostString is called *
* Current Activity (scSignup) is Paused *
* Activity Main is recreated *
* Activity Main is paused *

* Activity scSignup is recreated *

Below is the log:
Here it saves the signup settings internally
** Activity (scsignup) Pause, UserClosed = false **
Here it saves the signup state using StateManager, because Activity_Pause was called
Killing previous instance (main).
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **

Here it calls JobDone
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (b4xserializator_bytestoobject)
Killing previous instance (scsignup).
** Activity (scsignup) Create, isFirst = false **
running waiting messages (1)
** Activity (scsignup) Resume **
 

Marcus Araujo

Member
Licensed User
Longtime User
This tells me the problem is most likely in your JobDone event wherever it is, without seeing that code it is difficult to suggest a course of action.

I also thought on that. But see in the logs that JobDone event is called after the Main activity was recreated.
And my JobDone indeed makes use of CallSubDelayed, but the Component assigned is scSignup, not Main.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Again, without seeing the code, can't help you. Also, the order that the items appear in the log file is not necessarily the same as the order the statements themselves are executed.
 
Upvote 0

Marcus Araujo

Member
Licensed User
Longtime User
Again, without seeing the code, can't help you. Also, the order that the items appear in the log file is not necessarily the same as the order the statements themselves are executed.

When I pause the code inside JobDone, the activity that is currently being shown the Main activity.

Anyway, I think the code won't do much help, but here it is (I removed the logging stuff).
There is no way the JobName be different that the ones provided.

emptyMap is just an empty map, like CreateMap().
Utils is a code module that has some functions like json2map, map2json, etc. None of them call any subs.

B4X:
Private Sub JobDone(Job As HttpJob)
    Dim callModuleRef As Object 
    Select Job.JobName.ToUpperCase
        Case "DOWNLOADPICTURES"
            callModuleRef = Main
        Case "GETINFO"
            callModuleRef = Main
        Case "SENDSIGNUP"
            callModuleRef = scSignup
        Case "REQUESTORDERID"
            callModuleRef = scConfirmation
        Case "SENDORDER"
            callModuleRef = scConfirmation
    End Select

    If Job.Success Then
        Dim httpAns As String = Job.GetString
        If Utils.isValidJSON(httpAns) = False Then
            ReturnSub2(callModuleRef, "Server", Job.JobName, False, emptyMap)
        Else
            ReturnSub2(callModuleRef, "Server", Job.JobName, True, Utils.json2map(httpAns))
        End If
        Return 
    End If 
   
    ReturnSub2(callModuleRef, "Server", Job.JobName, False, emptyMap)
    Job.Release
End Sub

Private Sub ReturnSub2(mCallback As Object, mEventName As String, SubName As String, Arg1 As Boolean, Arg2 As Map)
    If SubExists(mCallback, mEventName & "_" & SubName) Then
        CallSubDelayed3(mCallback, mEventName & "_" & SubName, Arg1, Arg2)
    End If
End Sub
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Odds are you are either calling the Main or referencing an object declared in Main. Also, your .Release will never be called if your job was successful, not sure if that could cause this or not as I don't know what's under the hood for that.

Have you tried adding Log("Calling " & mcallback) just before your CallSubDelayed3 in ReturnSub2?
 
Upvote 0

Marcus Araujo

Member
Licensed User
Longtime User
Hm... I had an idea here, maybe here is the problem. I am reusing the same HttpJob object to make the calls. Maybe the JobName/Tag is getting messed up among them. I will try to queue them instead and post here the results.
 
Upvote 0

Marcus Araujo

Member
Licensed User
Longtime User
I implemented a Queue and made sure that Job.Release is called in all conditions... it still calls Main activity.

Log starting after moving to scSignup (HttpJob is in Starter service):
** Activity (main) Pause, UserClosed = false **
** Activity (scsignup) Create, isFirst = true **
** Activity (scsignup) Resume **

*User clicks in Send Signup*
[Starter, 10/25/2018 18:28:57] Processing next HTTP request. Size = 1
** Activity (scsignup) Pause, UserClosed = false **
Killing previous instance (main).
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **

[Starter, 10/25/2018 18:28:58] HTTPS (sendSignup): HTTP=OK, SUCCESS=OK
[Starter, 10/25/2018 18:28:58] Calling sub Server_sendSignup
[Starter, 10/25/2018 18:28:58] No more HTTP requests to process

** Activity (main) Pause, UserClosed = false **
Killing previous instance (scsignup).
** Activity (scsignup) Create, isFirst = false **
** Activity (scsignup) Resume **
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
*User clicks in Send Signup*
[Starter, 10/25/2018 18:28:57] Processing next HTTP request. Size = 1
** Activity (scsignup) Pause, UserClosed = false **
Killing previous instance (main).
Are you referencing something declared in Main in your button click event in the scSignup activity? Or does your starter service method you invoke from scSignup reference something in Main?
 
Upvote 0

Marcus Araujo

Member
Licensed User
Longtime User
Are you referencing something declared in Main in your button click event in the scSignup activity? Or does your starter service method you invoke from scSignup reference something in Main?

No, just a call to sending the HTTP request + showing the dialog (which was initialized in scSignup).
 
Upvote 0

Marcus Araujo

Member
Licensed User
Longtime User
After a thorough search through my code I found what was killing me... yes, there was a CallSubDelayed to Main in one of my codes. Removed that, bingo!

Thanks everyone for the answers.
 
Upvote 0
Top