Initializing variables when the Back key is pressed

isuru

Member
Licensed User
Longtime User
I'm having this issue in this app I'm writing. It would be confusing to explain it with all the unnecessary code and all. I'll just describe the problem taking an example.
Say there are two Activities. SearchActivity and ResultsActivity. SearchActivity has some members (like Dim lvResults As ListView) declared inside Globals.

When the user taps a button in SearchActivity, it executes a query, fetches some data and starts the ResultsActivity which displays that data in a ListView.
But If the user press the Back key and try that again, it throws the error Object should first be initialized (ListView). I guess that error pops up because the code under Globals don't get executes again when the Back key is pressed, right?

How can I avoid getting this error?

Thank you :)
 

isuru

Member
Licensed User
Longtime User
In SearchActivity, if the returned result set isn't empty, it starts the ResultsActivity

B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Dim res As String
   res = Response.GetString("UTF8")
   Dim parser As JSONParser
   parser.Initialize(res)
   Select TaskId
      Case Common.SEARCH_RESULTS
         ressulset = parser.NextArray
         If ressulset.Size = 0 Then
            StartActivity(NoResults)
         Else
            StartActivity(ResultsActivity)
         End If
   End Select
   Response.Release
End Sub

The code in the ResultsActivity. Yes in the Activity_Create, the layout is loaded into a Panel and it is added to the Activity.

B4X:
Sub Globals
   Dim ResultsPanel As Panel
   Dim lvResults As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      ResultsPanel.Initialize("ResultsPanel")
      ResultsPanel.LoadLayout("results")
      Activity.AddView(ResultsPanel, 0, 0, 100%x, 100%y)
   End If
   
   For i = 0 To Search.ressulset.Size - 1
      Dim m As Map
      m = Search.ressulset.Get(i)
      Dim tl As ThreeLines
      tl.First = m.Get("Name")
      tl.Second = m.Get("Cost")
      tl.Third = m.Get("SID")
      lvResults.AddTwoLines2(tl.First, "Rs. " & tl.Second & " per km", tl) 'the error occurs here
   Next
   ProgressDialogHide
End Sub

The first time it runs okay. The error comes when I press Back key and try it again.
If you're unclear on any part please let me know. Thanks :)
 
Upvote 0
Top