Android Question State Manager

Discussion in 'Android Questions' started by BarryW, Apr 18, 2015.

  1. BarryW

    BarryW Active Member Licensed User

    Hi masters... Why does state manager dont save sa text of button also does the state manager support sa items in listview? (save sa state of listview with items loaded)

    Here is my code

    Code:
    Sub Globals
        
    Dim Button1 As Button, Label1 As Label, EditText1 As EditText, ListView1 As ListView
        
    Dim TestInt As Int
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("1")
        
    Activity.AddMenuItem("Test Value""mnuTest")
       
        
    If StateManager.RestoreState(Activity"Main"0) = False Then
        
    End If
       
        
    Dim AutoUpdate As Boolean
        AutoUpdate = StateManager.GetSetting2(
    "AutoUpdate"False)
    End Sub

    Sub Activity_Resume
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
        
    If UserClosed Then
            StateManager.ResetState(
    "Main")
        
    Else
            StateManager.SaveState(
    Activity"Main")
        
    End If
        StateManager.SaveSettings
    End Sub

    Sub mnuTest_Click
        
    Msgbox(TestInt, "")
    End Sub

    Sub Button1_Click
        Button1.Text = 
    Rnd(1101)
        TestInt = EditText1.Text
        
    For x = 1 To TestInt
            ListView1.AddSingleLine(x)
        
    Next
       
        StateManager.SaveState(
    Activity"Main")
        StateManager.SaveSettings
    End Sub
    Tnx...
     

    Attached Files:

  2. DonManfred

    DonManfred Expert Licensed User

    What is "sa text"?
    What is/are sa items?

    See code from Stetmanager
    Code:
    Sub SaveState(Activity As Activity, ActivityName As String)
        
    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
        states.Put(ActivityName.ToLowerCase, list1)
        writeStateToFile
    End Sub
    Sub writeStateToFile
        
    Dim raf As RandomAccessFile
        raf.Initialize(
    File.DirInternal, statesFileName, False)
        raf.WriteObject(states, 
    True, raf.CurrentPosition)
        raf.Close
    End Sub
    Sub innerSaveState(v As View, list1 As List)
        
    Dim data() As Object
        
    If v Is EditText Then
            
    Dim edit As EditText
            edit = v
            data = 
    Array As Object(edit.Text, edit.SelectionStart)
        
    Else If v Is Spinner Then
            
    Dim spinner1 As Spinner
            spinner1 = v
            data = 
    Array As Object(spinner1.SelectedIndex)
        
    Else If v Is CheckBox Then
            
    Dim check As CheckBox
            check = v
            data = 
    Array As Object(check.Checked)
        
    Else If v Is RadioButton Then
            
    Dim radio As RadioButton
            radio = v
            data = 
    Array As Object(radio.Checked)   
        
    Else If v Is ToggleButton Then
            
    Dim toggle As ToggleButton
            toggle = v
            data = 
    Array As Object(toggle.Checked)
        
    Else If v Is SeekBar Then
            
    Dim seek As SeekBar
            seek = v
            data = 
    Array As Object(seek.Value)
        
    Else If v Is TabHost Then
            
    Dim th As TabHost
            th = v
            data = 
    Array As Object(th.CurrentTab)
            
    For i = 0 To th.TabCount - 1
                th.CurrentTab = i
            
    Next
            list1.Add(data)
            
    Dim data() As Object
            
    Dim r As Reflector
            r.Target = th
            
    Dim tabParentPanel As Panel
            tabParentPanel = r.RunMethod(
    "getTabContentView")
            
    For i = 0 To tabParentPanel.NumberOfViews - 1
                innerSaveState(tabParentPanel.GetView(i), list1)
            
    Next
        
    Else If v Is ScrollView Then
            
    Dim sv As ScrollView
            sv = v
            data = 
    Array As Object(sv.ScrollPosition)
            list1.Add(data)
            
    Dim data() As Object
            innerSaveState(sv.Panel, list1)
        
    Else If v Is Panel Then
            
    Dim panel1 As Panel
            panel1 = v
            
    For i = 0 To panel1.NumberOfViews - 1
                innerSaveState(panel1.GetView(i), list1)
            
    Next
        
    End If
        
    If data.Length > 0 Then list1.Add(data)
    End Sub
    There is no Button-support in it. Also there is no listview support.

    If you want to store the text of a button or th content of a listview then you need to create a object which hold the listview items (listview does not expose such list) and you need to extend the class to write (or later restore) your own itemlist.

    As for the button. You need to extend the class too to support button text
     
    lemonisdead likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    StateManager saves "user editable" data. Like EditText Text property or TabHost selected tab.

    The user cannot directly modify the button text so it is not saved. The same is true about ListView items.
     
    lemonisdead likes this.
  4. BarryW

    BarryW Active Member Licensed User

    Also can the statemanager save my global variables cause if the phone change orientation the grobal variable also has been reset...
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Use process global variables instead.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice