Android Question Finishing an Activity

Terradrones

Active Member
Licensed User
Hi

I need advice\help again please.

I have an Activity where the User can calculate the Bearing and Distance between 2 measured Points in the Field. This can be done with either a Total Station or GPS. If however the User has no Comss with his\her Equipment, the program brings up another Activity, where the Angles and Distances to the 2 Points can be entered manually.

My problem is that the first Activity that calls the Activity to read the variables continues, without waiting for the "Input" Activity to complete. Any way around this?

Field Activity:

B4X:
[/
Sub PolarJoinTS()
'    If FlagRead <> 1 And CGlobals.InstType < 10 Then
'        If CheckBubble() = -1 Then Return
'    End If
    If CGlobals.FlagRead = 0 Then CGlobals.FlagRead = 1 Else CGlobals.FlagRead = 0 ' CGlobals.FlagRead = 1 is reading , flgread = 0 activate when stop reading
    If CGlobals.FlagRead = 1 Then
        ReadPolar.Visible = True
        ReadPolarJoin.Text = "Stop"
        ReadInst.ReadAll()
        Log(ReadInst.HVD(1))
        Log(ReadInst.HVD(2))
        Log(ReadInst.HVD(3))
        If ReadInst.HVD(1) > -999999 And ReadInst.HVD(2) > -999999 And ReadInst.HVD(3) > 0 Then
            CGlobals.Ein(1) = ReadInst.HVD(1)
            CGlobals.Ein(2) = ReadInst.HVD(2)
            CGlobals.Ein(3) = ReadInst.HVD(3)
            CGlobals.Ein(4) = Hgt.Text
            CGlobals.FlagRead = 0
            ReadPolarJoin.Text = "Read"
            ReadPolar.Visible = False
            CalcPolarJoin
        Else
            ReadPolarJoin.Text = "Read"
            ReadPolar.Visible = False
        End If
    Else
        ReadPolarJoin.Text = "Read"
        ReadPolar.Visible = False
        'StopRead()
    End If
End Sub

]

        ReadInst.ReadAll()....this reads the Total Station\GPS, but if no Equipment has been found, it brings up the Activity to enter the readings manually:

[CODE=b4x][/
Sub ReadAll()
    If CGlobals.SCF = 0 Then CGlobals.SCF = 1
    HVD(1) = -999999 : HVD(2) = -999999 : HVD(3) = -999999
    Try
'        If CGlobals.InstType < 10 Then
'            'Onboard
'            Log(Floor(CGlobals.DF(3)))
'            Select Case Floor(CGlobals.DF(3))
'                Case 0, 1, 2
'                    'Nikon, Trimble & Fokus
'                    ' ReadInstruments(2, 2)
'                Case 3, 5
'                    'StoneX 0x11 (char 17) get the distance and angle, 0x00 (char 0) get the angle , 0x12 (char 18) stop measuring
'                    'ReadInstruments(2, Chr(17))
'                Case 4
'                    'Geomax Zipp20
'                    'ReadInstruments(2, "meas/all")
'            End Select
'        Else
'            'Handheld
'            If CGlobals.DF(3) > 0 Then
'                ' ReadTS_3()
'            Else
                ManualTSRead.ReadMode = "ReadAll"
                StartActivity("ManualTSRead")
            'End If

        'End If
    Catch
        'TraceToFile(String.Format("ReadInst-ReadAll-1: {0}", ex.ToString()), TraceTypes.Exception)
    End Try
End Sub
]

My problem is that it does not wait to finish "ManualTSRead", but carries on with "PolarJoinTS", where no data.

Here is the Code for "ManualTSRead":

[CODE=b4x][/
[CODE=b4x][/

#Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim ReadMode As String
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Panel1 As Panel
    Dim tbHA, tbVA, tbSlope As EditText
    Dim btExit, btOK As Button
    Dim KB As IME
    Dim AngleStatus=0 As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("ManualTSRead")
    CGlobals.FinishedReading=0
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

#End Region

#Region ********************************************************** Data Input ********************************************

Sub tbHA_FocusChanged(HasFocus As Boolean)
    If HasFocus =False Then
        If IsNumber(tbHA.Text) = False Then tbHA.Text=0
        tbHA.Text=NumberFormat2(tbHA.Text,1,4,4,False)
        Try
            If tbHA.Text<>"" Then CheckAngles(tbHA)
        Catch
            Log(LastException)
        End Try
    Else
        tbHA.SelectAll
        Sleep(0)
        KB.ShowKeyboard(tbHA)
    End If
End Sub

Sub tbVA_FocusChanged(HasFocus As Boolean)
    If HasFocus =False Then
        If IsNumber(tbVA.Text) = False Then tbVA.Text=0
        tbVA.Text=NumberFormat2(tbVA.Text,1,4,4,False)
        Try
            'If tbVA.Text<>"" Then CheckAngles(tbVA)
        Catch
            Log(LastException)
        End Try
    Else
        tbVA.SelectAll
        Sleep(0)
        KB.ShowKeyboard(tbVA)
    End If
End Sub

Sub tbSlope_FocusChanged(HasFocus As Boolean)
    If HasFocus =False Then
        If IsNumber(tbSlope.Text) = False Then tbSlope.Text=0
        tbSlope.Text=NumberFormat2(tbSlope.Text,1,3,3,False)
    Else
        tbSlope.SelectAll
        Sleep(0)
        KB.ShowKeyboard(tbSlope)
    End If
End Sub

#End Region

#Region **************************************************** Buttons ******************************************************

Sub btExit_Click
    CGlobals.FinishedReading=1
    ReadInst.HVD(1) = -999999 
    ReadInst.HVD(2) = -999999
    Activity.Finish
End Sub

Sub btOK_Click
    If ReadMode = "ReadAll" Then
        If tbHA.Text = "" And tbVA.Text = "" And tbSlope.Text = "" Then
            ReadInst.HVD(1) = -999999
            ReadInst.HVD(2) = -999999
            ReadInst.HVD(3) = -999999
            Activity.Finish
        Else If IsNumber(tbHA.Text) And IsNumber(tbVA.Text) And IsNumber(tbSlope.Text) Then
            ReadInst.HVD(1) = tbHA.Text
            ReadInst.HVD(2) = tbVA.Text
            ReadInst.HVD(3) = tbSlope.Text
            CGlobals.FinishedReading=1
            Activity.Finish
        Else
            Msgbox2Async("Enter Valid Values", "Error", "OK", "", "", Null,False)
            Wait For Msgbox_Result (Answ As Int)
        End If
    Else If ReadMode = "ReadAng" Then
        If tbHA.Text = "" And tbVA.Text = "" Then
            ReadInst.HVD(1) = -999999
            ReadInst.HVD(2) = -999999
            ReadInst.HVD(3) = -999999
            Activity.Finish
        Else If IsNumber(tbHA.Text) And IsNumber(tbVA.Text) Then
            ReadInst.HVD(1) = tbHA.Text
            ReadInst.HVD(2) = tbVA.Text
            ReadInst.HVD(3) = -999999
            CGlobals.FinishedReading=1
            Activity.Finish
        Else
            Msgbox2Async("Enter Valid Values", "Error", "OK", "", "", Null,False)
            Wait For Msgbox_Result (Answ As Int)
        End If
    Else If ReadMode = "ReadHAng" Then
        If tbHA.Text = "" Then
            ReadInst.HVD(1) = -999999
            ReadInst.HVD(2) = -999999
            ReadInst.HVD(3) = -999999
            Activity.Finish
        Else If IsNumber(tbHA.Text) Then 'AndAlso IsNumber(tbVA.Text) Then
            ReadInst.HVD(1) = tbHA.Text
            ReadInst.HVD(2) = -999999
            ReadInst.HVD(3) = -999999
            Activity.Finish
        Else
            Msgbox2Async("Enter Valid Values", "Error", "OK", "", "", Null,False)
            Wait For Msgbox_Result (Answ As Int)
        End If
    End If
End Sub

Sub CheckAngles(A As Double)
    Dim D0, D2, D3, S, ZCK As Double

    Try
        If CGlobals.DF(10) = 1 Then
            ZCK = A * 10 / 9
        Else
            ZCK = A
        End If
        If ZCK >= 360 Then ZCK = ZCK - 360
        If ZCK < 0 Then ZCK = ZCK + 360
        S = ZCK / (Abs(ZCK + 0.0000000000001)) : ZCK = Abs(ZCK)
        D0 = Floor(ZCK) : D2 = (ZCK - D0) * 100 : D3 = (D2 - Floor(D2)) * 100 : D2 = Floor(D2)
        If D2 > 59.9999 Or D3 > 59.9999 Then
            Msgbox2Async("Angle Not Entered Correctly", "Angle Error", "OK", "", "", Null,False)
            Wait For Msgbox_Result (Answ As Int)
            AngleStatus = -1
        Else
            AngleStatus = 1
        End If
    Catch
        Log(LastException)
    End Try
 End Sub
]


]
 
Last edited:

MicroDrie

Well-Known Member
Licensed User
Longtime User
You define
User has no Comss with his\her Equipment
For me it is unclear how you detect that situation. Your best bet is something to
  1. Set the GPS to high precision (and high power use!)
  2. Then read the current GPS location (what to do with no GPS or shutoff the GPS?)
  3. Ask the user to move
  4. Read the new GPS location
  5. Set the GPS in low precision (low power)
  6. Check for the difference between 2 and 4 as the bases to popup the manual location request
I think that the result of
Your code routine:
Sub ReadAll()
    If CGlobals.SCF = 0 Then CGlobals.SCF = 1
has nothing to do as a result of something that is based on a advised steps. It change a value while it is not based on a result of a test.

I advice you to start with a simple small program to implement this approach and make it first full prove to detect and handle correctly all wrong situations. With such approach you divide detection and presentation which make it easier to maintain and re-use your code in the future with differed equipment / phones. When programming it with B4A under a B4XPages solution, you can make, re-use, and add new functionality step by step with good maintainability and as a bonus, show us only the relevant part of your program code in a question.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I don't think he is asking about detecting that situation which I assume he has under control.

StartActivity doesn't wait. In fact it is only actioned when the Sub that called it eventually returns to the app message loop. Then the calling activity will be paused and the called activity created and brought to the front. This is the normal activity lifecycle on Android which you need to be familiar with.

Probably easier if you change to using B4XPages where you can switch between pages with no complications.
 
Upvote 0

Terradrones

Active Member
Licensed User
I solved the problem with "Sleep(0)"
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…