Tabhost/Statemanager problem

Jack Cole

Well-Known Member
Licensed User
Longtime User
I am having a problem using statemanager to persist data using a tabhost, with a scrollview for the tab panels. Essentially, it appears that statemanager does not save the data from the controls in the scrollview nested in a tabhost.

To reproduce the problem, download the attached example. Type some text in one of the text boxes and rotate the orientation of your device to force the view to be re-created.

Is this a limitation of statemanager?
 

Attachments

  • tabhoststatemgr.zip
    9.1 KB · Views: 362

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is this a limitation of statemanager?
Yes. However it shouldn't be difficult to modify the code a little bit and support the inner views of the TabHost.

The steps required are:
- Make sure to use TabHost.AddTab2 and add a Panel to each Tab (load the layout to the panel).
- Modify SaveState and RestoreState to receive an additional List with the panels:
B4X:
Sub SaveState(Activity As Activity, ActivityName As String, Panels As List)
   If states.IsInitialized = False Then states.Initialize
   Dim list1 As List
   list1.Initialize
   list1.Add(DateTime.Now)
   For i = 0 To Activity.NumberOfViews - 1
      innerSaveState(Activity.GetView(i), list1)
   Next
   For i = 0 To Panels.Size - 1 'NEW
      innerSaveState(Panels.Get(i), list1)
   Next
   states.Put(ActivityName.ToLowerCase, list1)
   writeStateToFile
End Sub

B4X:
Sub RestoreState(Activity As Activity, ActivityName As String, ValidPeriodInMinutes As Int, Panels As List) As Boolean
   Try
      loadStateFile
      If states.IsInitialized = False Then
         Return False
      End If
      Dim list1 As List
      list1 = states.Get(ActivityName.ToLowerCase)
      If list1.IsInitialized = False Then Return
      Dim time As Long
      time = list1.Get(0)
      If ValidPeriodInMinutes > 0 AND time + ValidPeriodInMinutes * DateTime.TicksPerMinute < DateTime.Now Then
         Return False
      End If
      listPosition = 0
      For i = 0 To Activity.NumberOfViews - 1
         innerRestoreState(Activity.GetView(i), list1)
      Next
      For i = 0 To Panels.Size - 1 'NEW
         innerRestoreState(Panels.Get(i), list1)
      Next
      Return True
   Catch
      Log("Error loading state.")
      Log(LastException.Message)
      Return False
   End Try
End Sub

- Create a list with the tabs panels and pass it to SaveState and RestoreState.

I haven't tested it so be prepared for surprises...
 
Upvote 0
Top