Android Question Problem handling multiple activities?

Mohamed Akbarally

Member
Licensed User
Longtime User
Hi all,
I'm having a bug. my program has multiple activities but my main activities the problem every time debug the program or launch the program (activity_create) and then switch to another activity. that activity loads and the within seconds with out pressing anything the main activity pops up again. if anyone knows a solution or has had this same problem, please tell me a fix? i have tried activity.finish

*also i'm using httputils2 to load some websites on the main activity if this is the problem please tell me how to stop the function when switching to different activities.
 

Mohamed Akbarally

Member
Licensed User
Longtime User
Is there an is easier way to do this because all i'm doing with httputils is getting the source code of a webpage and then finding a substring of that. can this be done with webview extras
B4X:
'global
dim job1 as httpjob
'activity create
    job1.Initialize("Job1", Me)
    job1.Download("https://weather.yahoo.com/sri-lanka/western/colombo")
'sub job done
Sub JobDone (Job As HttpJob)

  If Job.Success = True Then
  Indexsearch = "<span class=" & Chr(34) & "c" & Chr(34) & "><span class=" & Chr(34) & "num" & Chr(34) & ">"
  Sourcecode = job1.GetString
  Indexofsource = Sourcecode.IndexOf(Indexsearch)
  Indexofsource = Indexofsource + 35
  weather = Sourcecode.SubString2(Indexofsource -1 ,Indexofsource + 1)
  Label5.Text = weather & Chr(176)
End If
End Sub
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
with HTTP library ..

B4X:
Sub Globals
   Dim hc As HttpClient
   Dim req As HttpRequest
End Sub

Sub Whatever ...
  hc.Initialize("hc")    
  req.InitializeGet("https://weather.yahoo.com/sri-lanka/western/colombo")
  hc.Execute(req, 1)
end Sub

Sub hc_ResponseSuccess(Response As HttpResponse, TaskId As Int)
                  
   Dim TR As TextReader
   TR.Initialize(Response.GetInputStream)  ' for ICS might have run response.GetAsynchronously ??

   'find a starting point ...
   Dim pattern, line As String
   pattern = "<span class=" & Chr(34) & "c" & Chr(34) & "><span class=" & Chr(34) & "num" & Chr(34) & ">"
  
   Do Until line.Trim = pattern
     line = TR.ReadLine
   Loop

   'go down 2 more lines ..
   line = TR.ReadLine
   line = TR.ReadLine
   myString = line.Trim

   TR.Close

End Sub

Sub hc_ResponseError(Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)

   Msgbox("Error: " & Response.GetString("UTF8"), "Error Accessing Web Page ...")
      
End Sub
 
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Erel .. maybe its time for an ' Unlike ' button .. back to the drawing board :(
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't get me wrong. Your code is a good start for someone who wants to use Http directly.

In almost all cases it is simpler to use HttpUtils2. Now HttpUtils2 is not so sophisticated. You can see its source code here: http://www.b4x.com/android/forum/threads/httputils2-web-services-are-now-even-simpler.18992/

Many developers try to avoid using services. It is a mistake. Services are simpler than activities. Their life cycle is much simpler to manage.
This is especially true when there are multiple activities.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Erel .. thanks for the clarification. I will have a read of the link you supplied. more for the fact of encountering possible errors.

@Mohamed Akbarally .. I initially avoided services but soon realized how simple they were to work with, as Erel states above
 
Upvote 0
Top