Android Question [SOLVED] Statemanager not saving?

alexwekell

Member
Licensed User
Longtime User
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim checkedbox As Int = 0

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
 
    Dim ad As ActionDrawer
    Dim test As Label
    Dim options1 As ListView
    Dim transbars As ImageView
    Dim robotobold As Typeface

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
 
    If StateManager.RestoreState(Activity, "settingsmenu", 0) = False Then
        checkedbox = 0
    Else If StateManager.RestoreState(Activity,"settingsmenu", 0) = True Then
        checkedbox = 1
    End If
 
    robotobold = Typeface.LoadFromAssets("Roboto-Condensed.ttf")

Dim Overflow4 As List
    Overflow4.Initialize
    Overflow4.Add("Source")
 
    Dim robotocon As Typeface
    robotocon = Typeface.LoadFromAssets("Roboto-Bold.ttf")
 
    ad.Initialize("EM-Direct", "ic_icon.png", True, Colors.RGB(213,213,213), "light", False, True, False, Overflow4, "None", Activity, Me, "Settings")
 
'    'If you want to use a light theme add "SetApplicationAttribute(android:theme, "@android:style/Theme.Holo.Light")" to the manifest
' 
    ad.AppTitle = "EM-Direct"

Dim s As ScrollView
s.Initialize(1000dip)
Dim c As Canvas


s.Color = Colors.Transparent
Activity.AddView(s, 0dip, 73dip, 500dip, 500dip)

s.Panel.Width = 1000dip
s.Panel.Height = 1000dip

test.Initialize("")
transbars.Initialize("transbars")
test.text = "VIEW SETTINGS"
test.Typeface = robotocon
test.TextSize = 14
test.TextColor = Colors.DarkGray
s.Panel.AddView(test, 30dip, 10dip, 400dip, 400dip)
s.Panel.AddView(transbars, 100%x-70dip, 55dip, 30dip, 30dip)

transbars.BringToFront

options1.Initialize("Options1")

options1.SingleLineLayout.Label.TextColor = Colors.DarkGray
options1.SingleLineLayout.Label.TextSize = 18
options1.AddSingleLine("Translucent Bars")
options1.SingleLineLayout.ItemHeight = 60dip

If checkedbox = 0 Then
transbars.SetBackgroundImage(LoadBitmap(File.DirAssets, "apptheme_btn_check_off_holo_light.png"))
Else
transbars.SetBackgroundImage(LoadBitmap(File.DirAssets, "apptheme_btn_check_on_holo_light.png"))
End If

transbars.Gravity = Gravity.FILL


s.Panel.AddView(options1, 20dip, 40dip, 100%x-40dip, 100dip)

c.Initialize(s.Panel)
c.DrawLine(20dip, 40dip, 100%x-20dip, 40dip, Colors.gray, 2dip)

End Sub

Sub Activity_Resume

End Sub

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


Sub transbars_clicked

Dim checkboxcolor As BitmapDrawable

If checkedbox = 0 Then
        checkedbox = 1
        checkboxcolor.Initialize(LoadBitmap(File.DirAssets,"apptheme_btn_check_on_holo_light.png"))
        transbars.Background = checkboxcolor
    Else
        checkedbox = 0
        checkboxcolor.Initialize(LoadBitmap(File.DirAssets,"apptheme_btn_check_off_holo_light.png"))
        transbars.Background = checkboxcolor
End If

End Sub

Sub Options1_ItemClick (Position As Int, Value As Object)

Dim checkboxcolor As BitmapDrawable

If Value = "Translucent Bars" Then
    If checkedbox = 0 Then
        checkedbox = 1
        checkboxcolor.Initialize(LoadBitmap(File.DirAssets,"apptheme_btn_check_on_holo_light.png"))
        transbars.Background = checkboxcolor
    Else
        checkedbox = 0
        checkboxcolor.Initialize(LoadBitmap(File.DirAssets,"apptheme_btn_check_off_holo_light.png"))
        transbars.Background = checkboxcolor
    End If
End If

End Sub

Transbars is a sudo-checkbox (I use an imageview so I can use a custom checkbox color) that should enable and disable translucent bars in my app. The code itself works, but the checkbox state isn't kept when the app is closed? When the app starts the checkbox is unchecked and if I check it and leave the app and come back, it becomes uncheck, what am I doing wrong?

checkedbox = 0 is "unchecked"

Edit: Was using backbutton, my bad.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
on exit the app
B4X:
If UserClosed Then
        StateManager.ResetState("settingsmenu")
you reset the saved state... So on next start the state is not available and checkedbox becomes 0
 
Upvote 0
Top