Using Map

JonPM

Well-Known Member
Licensed User
Longtime User
Fairly new here and been trying to work with Map all day. I am trying to achieve a couple things. First is I want to save user settings in my app (specifically units of measure: Lbs or Kg), and also the status of a checkbox. I will also need to store the strings of edittext boxes and later retrieve those strings.

Here's my code regarding the checkbox:


Sub Process_Globals
Dim Map1 As Map
End Sub

Sub Globals
Dim chkFavStartup As CheckBox
Dim settingsFav As String
End Sub

Sub Activity_Create(FirstTime As Boolean)

If FirstTime Then
Map1.Initialize
Map1.Put("settingsFav","False")
File.WriteMap(File.DirInternal,"Map.txt",Map1)
End If

Activity.LoadLayout("main")

Map1 = File.ReadMap(File.DirInternal,"Map.txt")
chkFavStartup.Initialize("")
settingsFav = Map1.Get("settingsFav")
If settingsFav = "True" Then
chkFavStartup.Checked = True
Else
chkFavStartup.Checked = False
End If


Sub btnSaveSettings_Click
Map1.Initialize
If chkFavStartup.Checked = True Then
Map1.Put("settingsFav","True")
Else
Map1.Put("settingsFav","False")
End If
File.WriteMap(File.DirInternal, "Map.txt", Map1)

ToastMessageShow("Settings Saved",False)

End Sub

So I'm not getting any errors, but it's not working either. I have the app installed on my phone, but the checkbox won't stay checked when I restart the app. What am I doing wrong?
 

kickaha

Well-Known Member
Licensed User
Longtime User
How are you quitting the app?

If the app has been fully stopped, when you "restart" it, the FirstTime flag in Activity_Create will be true and you will overwrite the saved settings. The solution is to check for the existance of the file, and only write it if it does not exist.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
How are you quitting the app?

I am using the back button. Do I have to somehow close the file before the app closes? Whats the coding to do that?

Erel, will look into state manager. Whats the benefit over maps?


Sent from my DROIDX using Tapatalk
 
Upvote 0

Brad

Active Member
Licensed User
Longtime User
Here's your problem:
B4X:
If FirstTime Then
Map1.Initialize
Map1.Put("settingsFav","False")
File.WriteMap(File.DirInternal,"Map.txt",Map1)
End If

Every time you launch the program it sets the state to false. Also, If you are writing/reading only one setting I would use file.WriteString/file.ReadString. I find it much easier and simpler to use.

Edit: oops..kickaha caught it too.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Thanks, see it now. I will be saving multiple settings. I just put one of them up for simplicity.

Sent from my DROIDX using Tapatalk
 
Upvote 0
Top