Android Question [Solved]StdViewPager and Statemanager?

anOparator

Active Member
Licensed User
Longtime User
The StateManager.zip sample restores an Edittext which is on a Panel when the screen is rotated, but when I use StateManager.bas with StdViewPager I'm not finding how to restore the Edittext in my project.
I've tried passing StateManager the Panel instead of the activity as the first parameter. (as listed below)
B4X:
Sub Activity_Create(FirstTime As Boolean)
...
StateManager.RestoreState(Activity, "Main)", 60)
StateManager.RestoreState(Activity, "Panels(svpTest.CurrentPage)", 60)
StateManager.RestoreState(Activity, "svpTest.Panels(svpTest.CurrentPage)", 60)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
     StateManager.SaveState(Activity, "Main")
     StateManager.SaveState(Activity, "Panels(svpTest.CurrentPage")
     StateManager.SaveState(Activity, "svpTest.Panels(svpTest.CurrentPage")
End Sub
Is there a way to get around the initialization after a screen rotation which resets 'CurrentPage' to zero, or what other way is there to get this to work?
Thanks in advance
 

Attachments

  • svpStateMgr.zip
    13.1 KB · Views: 186

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change your code to:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   pnlTest.Initialize("")  ' screen rotation sets the current page to zero
   svpTest.Initialize("svpTest", 4, 100%x, 100%y)
   Activity.AddView(svpTest.AsView, 0, 0, 100%x, 100%y)
   Log("Create CurrentPage =  " & svpTest.CurrentPage)
   svpTest.Panels(3).AddView(pnlTest, 0,0, 100%x, 100%y)   
   svpTest.Panels(0).LoadLayout("0")
   svpTest.Panels(1).LoadLayout("1")
   svpTest.Panels(2).LoadLayout("2")
   svpTest.Panels(3).LoadLayout("3")
   For i = 0 To svpTest.Panels.Length - 1
     StateManager.RestoreState(svpTest.Panels(i), i, 60)
   Next
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   For i = 0 To svpTest.Panels.Length - 1
     StateManager.SaveState(svpTest.Panels(i), i)
   Next
End Sub
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
Thanks, I see the 'svpTest.Panels(3).AddView(pnlTest, 0,0, 100%x, 100%y)' thingy and have added the For loops but it still goes to page 0 when
the screen rotates. I even tested with 'StateManager.SaveSettings' after Next in Sub Activity_Pause.
What code could be missing?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The code I posted saves the panels state. It doesn't save the current selected page. It is very simple to add it:

B4X:
Sub Activity_Pause (UserClosed As Boolean)
 StateManager.SetSetting("CurrentPage", svpTest.CurrentPage)
End Sub

Sub Activity_Create(FirstTime As Boolean)
 ...
 svpTest.ScrollTo(StateManager.GetSetting2("CurrentPage", 0), False)
End Sub
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
The code I posted saves the panels state. It doesn't save the current selected page. It is very simple to add it:

B4X:
Sub Activity_Pause (UserClosed As Boolean)
 StateManager.SetSetting("CurrentPage", svpTest.CurrentPage)
End Sub

Sub Activity_Create(FirstTime As Boolean)
 ...
 svpTest.ScrollTo(StateManager.GetSetting2("CurrentPage", 0), False)
End Sub
Yes, basically beautiful powerful and simple. Thanks.
 
Upvote 0
Top