Android Question How to save settings?

Discussion in 'Android Questions' started by trueboss323, Feb 21, 2015.

  1. trueboss323

    trueboss323 Active Member Licensed User

    How can I save my settings inside my app. In VB.NET you use
    Code:
    My.Settings
    and you can change what type of value (String, Boolean, Integer, etc)
    What is the easiest way I can do that in B4A ?
     
  2. eurojam

    eurojam Well-Known Member Licensed User

  3. trueboss323

    trueboss323 Active Member Licensed User

    Hi,
    I looked at the library. Could you please provide an example?
    Say that I want to save a simple textbox like My.Settings.text = TextBox1.Text
    and then load it like: TextBox1.Text = My.Settings.text
    Whats the best way i can do that?
     
  4. sorex

    sorex Expert Licensed User

    StateManager is very easy to use.
     
  5. trueboss323

    trueboss323 Active Member Licensed User

    Thanks. I'm still trying to figure out how I could save booleans like checkboxes, I can't seem to find the proper code for saving, and loading them.
    Could you provide some help please ?
     
  6. sorex

    sorex Expert Licensed User

    I never worked with checkboxes but I guess it will be something like

    Code:
    'load or set first time defaults
    StateManager.loadStateFile
    chkOption1.checked=StateManager.GetSetting2(
    "option1",0)

    'saving
    StateManager.SetSetting("option1",chkOption1.checked)
    StateManager.SaveSettings
     
  7. trueboss323

    trueboss323 Active Member Licensed User

    I have tried your code and unfortunately that didn't work for me.
     
  8. sorex

    sorex Expert Licensed User

    worst case you can solve it with if/then's

    Code:
    'load or set first time defaults
    StateManager.loadStateFile
    if StateManager.GetSetting2("option1",0)=0 then
    chkOption1.checked=
    false
    else
    chkOption1.checked=
    true
    end if

    'saving
    if chkOption1.checked=false then
    StateManager.SetSetting(
    "option1",0)
    else
    StateManager.SetSetting(
    "option1",1)
    end if
    StateManager.SaveSettings
     
  9. trueboss323

    trueboss323 Active Member Licensed User

    Thank you! That one worked. And do you also know how I can do the same with text and labels? Since I'm not sure if StateManager.SetSetting("text1",1) would work with Strings.
     
  10. sorex

    sorex Expert Licensed User

    the first is the paraeter name, the second the value so you could use

    Code:
    StateManager.SetSetting("text1",editText1.text)
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    StateManager SetSettings is indeed very simple to use. However you should also check KeyValueStore class which is simple and much more powerful.
     
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