Android Question Activity closes following CallSub

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I'm going crazy with this problem.

I have a second activity in an app which makes CallSubs to a service in response to a button click. When button click routine exits, for some reason the second activity pauses and the main activity resumes.

Below is a little snippet of code (I can't post the full code) and the output I receive in the unfiltered logs when executing this portion of the code.

B4X:
  si(1) = TchRec.l
  si(2) = TchRec.t
  si(3) = TchRec.r
  si(4) = TchRec.b
   
  For i = 0 To sa.Length - 1
    si(0) = sa(i)
   
    Dim b() As Byte
   
    b = bc.ShortsToBytes(si)
   
'    CallSubDelayed3(NetSvr, "SendCmd", NetSvr.C_VPInfo, b)
    CallSub3(NetSvr, "SendCmd", NetSvr.C_VPInfo, b)
  Next

B4X:
Activity (vpsact) Pause, UserClosed = false **
Killing previous instance (main).
** Activity (main) Create, isFirst = false **
Class not found: canalrun.apps.shuttle.txtwnd, trying: canalrun.apps.t3host.txtwnd
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **

In the code, note that I've tried both CallSubDelayed3 and CallSub3. One or the other is commented. If I comment both, the activity doesn't pause and main is not restarted.

The first two lines of the unfiltered log output show what happens. The activity vpsact is paused for some unknown reason and the main activity is restarted.

The NetSvr service is almost identical to Erel's tutorial NetSvc. The first parameter is a byte. The second parameter is a byte array.

Any ideas or places I should look to find out why CallSub seems to be pausing the activity?

Thanks,
Barry.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
I'm wondering if it's got something to do with the service being started from the first activity. I have a network service in one of my apps which I access from the activity that started it, plus a class module - but I don't try to access it from another activity, so I don't know if this is an issue.

What does the "SendCmd" sub do in your service? I doesn't make a call to a sub in your first activity by any chance?
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
I'm wondering if it's got something to do with the service being started from the first activity. I have a network service in one of my apps which I access from the activity that started it, plus a class module - but I don't try to access it from another activity, so I don't know if this is an issue.

What does the "SendCmd" sub do in your service? I doesn't make a call to a sub in your first activity by any chance?

B4X:
Sub SendCmd(Cmd As Byte, Data() As Byte)
  Dim buf(1 + Data.Length) As Byte
 
  buf(0) = Cmd
  bc.ArrayCopy(Data, 0, buf, 1, Data.Length)
 
  AStrm.Write(buf)
End Sub

Good comment. Nope. It Does not make any calls to the main activity (the one that started the service).

The service was started in the main activity. The main activity makes several calls to SendCmd, without issue.

I also tried using a CallSub to call back to the main activity and then have the main activity call the SendCmd in the NetSvr service. This produced the same error.

My NetSvr service is almost identical to the NetSvc (I think it's called) service found in one of Erel's tutorials.

Barry.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
I did quite a bit of testing by commenting out different sections of code and adding dummy statements, like the following:

B4X:
'    CallSubDelayed3(NetSvr, "SendCmd", NetSvr.C_VPInfo, b)
'    CallSub3(NetSvr, "SendCmd", NetSvr.C_VPInfo, b)
    CallSubDelayed3(Main, "Dummy", NetSvr.C_VPInfo, b)

Where "Dummy" is a subroutine in the Main activity that does nothing. It would fail consistently as described in post #1 if I uncommented either of the two CallSub's to to the NetSvr service module, but did not fail in any test where these two CallSubs to NetSvr were commented out. I can successfully make the CallSub to NetSvr from the main activity.

I found a workaround by creating a Process_Globals List that I populate with the info I would've sent to NetSvr. When the Main activity resumes I check the list for contents and make the CallSubs to NetSvr from the Main Activity_Resume subroutine.

The workaround is satisfactory now, but I can forsee a time in the near future when I will need to use the results of the NetSvr CallSub before resuming the Main activity.

Can you imagine that there might be some problem making a CallSub from a second activity (the Main activity will be paused) to a service that was started within the Main activity? This is what seems to be happening.

Barry.
 
Last edited:
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Phew! I think software was invented to slowly drive people crazy.

I was developing a small test program when I discovered the problem. The SendCmd sends data that causes the receiver to return log text which is then displayed in a label in the Main activity.

The 2nd activity sends data to another device, the other device returns log information saying I got it, that causes the received data to be displayed in a label in the Main activity, this restarts the main activity... It looks like it's in error, but is just doing what it was told.

Barry.
 
Upvote 0
Top