Saving and reading the settings done with PreferenceManager

William Hunter

Active Member
Licensed User
Longtime User
Is there a code example somewhere on the forum illustrating how PreferenceManager is used to both save settings and then retrieve those settings as a means of configuring variables? I need a little more detailed information than is in the tutorial.

If someone has a code snippet they could post, their help would be greatly appreciated. :sign0104:
 

William Hunter

Active Member
Licensed User
Longtime User
PreferenceManager

Thank you for your response Penko. I did find that tutorial, but I need to see a code example demonstrating usage as in the following quote from the tutorial: "Reading the settings is done with PreferenceManager. To retrieve the value of a specific key you should use Manager.GetString or Manager.GetBoolean with this key." I would like to see code saving the settings and also reading those settings. I just need a little more in detail. I have no idea what the syntax would look like, or where the settings are stored.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
PreferenceManager

Here:
B4X:
manager.SetString("edit1", "Hello!")
Dim s As String
s = manager.GetString("somekey")
The settings are stored in the internal folder. Though you do not need to access the file yourself.
Hello Erel - Thank you for your response. I think I have some understanding of adding preferences, but I still need more guidance. I have a few more questions:

1. In Sub Activity_Create - is "If File.Exists(File.DirInternal, Manager.GetString("edit1")) = False Then" the correct method of checking for the existance of preference entries?

2. In Sub Activity_Resume - what should replace the empty strings for edit1, edit2 and edit3?

The problems I am having are:

1. The default entries do not appear in the preference screens.

2. If my method of checking for the existance of preference entries is correct, the preference settings I wish to make are not being stored.

Regards

These are my subs:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      Activity.LoadLayout("maintablet7")
       CreatePreferenceScreen
      If Manager.GetAll.Size = 0 Then SetDefaults
      If File.Exists(File.DirInternal, Manager.GetString("edit1")) = False OR File.Exists(File.DirInternal, Manager.GetString("edit2")) = False OR File.Exists(File.DirInternal, Manager.GetString("edit3")) = False OR File.Exists(File.DirInternal, Manager.GetBoolean("check1")) = False Then
         StartActivity(Screen.CreateIntent)
      Else
         PopServer = Manager.GetString("edit1")
         UserName = Manager.GetString("edit2")
         Password = Manager.GetString("edit3")
         reqSSL = Manager.GetBoolean("check1")
      End If
   End If
End Sub

Sub Activity_Resume
   Manager.SetString("edit1", "")
   Manager.SetString("edit2", "")
   Manager.SetString("edit3", "")
   Manager.SetBoolean("check1", False)
End Sub

Sub CreatePreferenceScreen
   Screen.Initialize("Settings", "")
   'create one category
   Dim cat1 As PreferenceCategory
   cat1.Initialize("POP3 Settings")
   cat1.AddEditText("edit1", "Server Address", "", "")
   cat1.AddEditText("edit2", "User Name", "", "")
   cat1.AddEditText("edit3", "Password", "", "")
   cat1.AddCheckBox("check1", "Check to Use SSL", "", False)
   'add the category to the settings screen
   Screen.AddPreferenceCategory(cat1)
End Sub

Sub SetDefaults
   'defaults are only set on the first run.
   Manager.SetString("edit1", "Server")
   Manager.SetString("edit2", "UserName")
   Manager.SetString("edit3", "Password")
   Manager.SetBoolean("check1", False)           
End Sub
 
Last edited by a moderator:
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
PreferenceManager

1. No. This is not the correct way. You can check if a value is set with:
B4X:
If manager.GetString("edit1") <> "" Then
...
Hello Erel – Thank you for your help along the way. I have tried this line of code as a means of validating that Preference settings exist on first run. Unfortunately, the Preference Screen is brought up whether the Preference settings exist or not.

I would prefer a Boolean check, but I’m thinking this may not be possible. If I knew the FilePath to a txt or ini file this could be accomplished, but I’m thinking the settings may be stored in an xml file. If so, and the file is created at the same time the Preference Screen is created. Then a validation check at this time would serve no purpose, as the settings would not have yet been set.

Do you have any other thoughts on this?

Regards

I have found a solution to my problem, by changing the code to:
B4X:
If Manager.GetString("edit1") = "Default Text Here" Then
 
Last edited:
Upvote 0
Top