Android Question StateManager not working for me.

alexwekell

Member
Licensed User
Longtime User
I have two activities (along with some others, but I only want to store settings):

"Main"
"Settingsmenu"

In activity_create of Main, at the very bottom:

B4X:
StateManager.RestoreState(Activity, "settingsmenu", 60)

In Activity_pause of settingsmenu:

B4X:
Sub Activity_Pause (UserClosed As Boolean)
    StateManager.SaveState(Activity, "settingsmenu")
    StateManager.SaveSettings
End Sub

It work's when I back out or press home, but if I force close of the app or restart my device, none of the settings come back (which sort of defeats the purpose of a settings menu)

Any help?
 

alexwekell

Member
Licensed User
Longtime User
This is how Android works. If you kill the app with a task manager or restart the device then no event is fired.

This is of course not a standard use case. If you like you can store the state every x seconds with a timer. Though if the user manually kills the app then it is probably better to reset the state.
So then what's the best way with B4A to permanently store settings? I need the settings to last in the event that the phone is shutoff, app closed, app swiped away, etc.
 
Upvote 0

alexwekell

Member
Licensed User
Longtime User
Call StateManager.SaveSettings after each change.

Or better, use KeyValueStore to store the settings.

Can't seem to get KeyValueStore to work either:

Main:

B4X:
If FirstTime Then
    kvs.Initialize(File.DirDefaultExternal,"datastore")
End If

B4X:
  If p.SdkVersion = 19 Then
  Dim bartop As ImageView
  bartop.Initialize("")
  Dim barbottom As ImageView
  barbottom.Initialize("")

  transbarflag  = kvs.GetSimple("transflag")

  If transbarflag = 1 Then
          bartopcolor = "bartopblack.jpg"
        barbottomcolor = "bartopblack.jpg"
  Else If transbarflag = 0 Then
          bartopcolor = "bartop.jpg"
        barbottomcolor = "bartrans.png"
    Else
          bartopcolor = "bartopblack.jpg"
        barbottomcolor = "bartopblack.jpg"
  End If
  
  bartop.Bitmap = LoadBitmap(File.DirAssets, bartopcolor)
  barbottom.Bitmap = LoadBitmap(File.DirAssets, barbottomcolor)
  Activity.AddView(bartop, 0dip, 0dip, 100%x, 25dip)
  Activity.Addview(barbottom, 0dip, 100%y-48dip, 100%x, 48dip)
  bartop.Gravity = Gravity.FILL
  bartop.BringToFront
  bartop.Enabled = True
  bartop.Visible = True
  bartop.Invalidate
  barbottom.Gravity = Gravity.FILL
  barbottom.BringToFront
  barbottom.Enabled = True
  barbottom.Visible = True
  barbottom.Invalidate
  End If

Settings menu:

B4X:
Sub Options1_ItemClick (Position As Int, Value As Object)

Dim p As Phone

Dim checkboxcolor As BitmapDrawable

If Value = "Translucent Bars" Then

'If p.SdkVersion <= 18 Then
'    Msgbox("Sorry this feature requires a higher version of Android (KitKat, 4.4+)", "Error")
'Else If p.SdkVersion >= 19 Then
    If checkedbox = 0 Then
        checkboxcolor.Initialize(LoadBitmap(File.DirAssets,"apptheme_btn_check_on_holo_light.png"))
        transbars.Background = checkboxcolor
        checkedbox = 1
        Main.kvs.PutSimple("transflag", 1)
    Else
        checkboxcolor.Initialize(LoadBitmap(File.DirAssets,"apptheme_btn_check_off_holo_light.png"))
        transbars.Background = checkboxcolor
        checkedbox = 0
        Main.kvs.PutSimple("transflag", 0)
    End If
    Msgbox2("Changes will not go into effect until the app is restarted. To fully restart press back until you've exited the app.", "Translucent Bars", "", "Ok", "", Null)
End If


End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Do you expect KeyValueStore to update the ToggleButton state? It will not do it automatically. KeyValueStore just saves data.

You will need to read the value in Activity_Create and set the user interface state.

It is good for saving settings. If you want to save the interface state then it will be easier with StateManager.
 
Upvote 0
Top