iOS Question when the app is killed textfield.text value is lost

Baris Karadeniz

Active Member
Licensed User
When I write some values to the textfield, it holds the value at the foreground and background. But if app is killed and run again, the value is lost and textfield comes back empty. How can I provide textfield holds the value entered although app is killed?
 

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

I recommend you the following:

1. Save the value in the Application_Background sub:
B4X:
Private Sub Application_Background
    File.WriteMap(File.DirDocuments,"user",CreateMap("username":TextField1.Text))
End Sub

2. Read the value in the Start sub, and set it to the textfield:
B4X:
Private Sub Application_Start (Nav As NavigationController)
    TextField1.Text = File.ReadMap(File.DirDocuments,"user").Get("username")
End Sub

Or have a look here

Jan
 
Last edited:
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Ohh, I have forgotten this case.
Check if the file "user" exists, before you try to read the information:

B4X:
Private Sub Application_Start (Nav As NavigationController)
    If File.Exists(File.DirDocuments,"user") Then
            TextField1.Text = File.ReadMap(File.DirDocuments,"user").Get("username")
        Else
            TextField1.Text = "default value"
    End If
End Sub

Jan
 
Upvote 0

Baris Karadeniz

Active Member
Licensed User
It is not related to this topic but may I ask another question about text field? If the entered value into the TextField includes characters, it will not be accepted. I wrote the codes below. I used AnyChr as Char, but there is a log. It says "Variable 'AnyChar' is never assigned any value. (warning#10) How can I solve this?

B4X:
Dim AnyChr As Char
    If TextFieldimei.Text.Contains(AnyChr) Or TextFieldimei.Text.Length <> 15 Then
        Msgbox(warning2, warningtitle1)
        TextFieldimei.Text = ""
        Else
        File.WriteMap(File.DirDocuments,"user",CreateMap("userimei":TextFieldimei.Text))
    End If
End Sub
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Ah, ok.
Use IsNumber:
B4X:
    If Not(IsNumber(TextFieldimei.Text)) Or TextFieldimei.Text.Length <> 15 Then
        Msgbox(warning2, warningtitle1)
        TextFieldimei.Text = ""
        Else
        File.WriteMap(File.DirDocuments,"user",CreateMap("userimei":TextFieldimei.Text))
    End If

Jan
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…