Save the Value of a Text Field

Is there any way to save the value of a text field and then load that value when you reopen the app? Similar to SharedPreferences functionality in Android Studio? I tried loading from and saving to a text file but I got MD5# error when exporting my app.

  1. Yes. Just save the value as raw text in a file or as part of a JSON encoded table of values.

  2. The other problem you mentioned must be something wrong in your file writing code or maybe the permissions you used.

SSK2 contains io.* and table.* extensions to handle saving and restoring text and tables respectively.
https://roaminggamer.github.io/RGDocs/pages/SSK2/extensions/

You can grab just the extension files and require them directly as modules to extend io.* and table.* i.e. You donā€™t need to use all of SSK2.

This is easy peasy with SSK2. Here is a working example:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2020/07/nativeTextFields_restore.zip

Here is the important code:

require "ssk2.loadSSK"
_G.ssk.init()
-- =============================================================
local group = display.newGroup()

local saved_fields = table.load('saved_fields.json') or {}

local listener = function( self, event )
	saved_fields[self.myNum] = self.text 
	table.save( saved_fields, 'saved_fields.json') 
end
 
for i = 1 , 7 do 
	local tmp = native.newTextField( display.contentCenterX, 
		display.contentCenterY - display.actualContentHeight/4 + i * 60, 200, 55 )
	group:insert(tmp)

	tmp.myNum = i
	tmp.text = saved_fields[i] or ""
	tmp.id = i
	tmp.font = native.newFont( native.systemFontBold, 36 )
	tmp.align = "left"
	tmp.isEditable = true
	tmp.userInput = listener
	tmp:addEventListener( "userInput" )		
end

If you do decide to include SSK2 in your project, you could use persist instead.
https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/persist/

It has the added benefit of defering writes so that multiple persist.set() calls in a short span of time result in a single ā€˜storageā€™ write instead of multiple (to save battery life).

table.save() and table.load() donā€™t wait. Still, they are fine for most uses.

Hello,

another option I can think of might be save it as preference. Here is a link to old blog.

Here is reference to system.setPreference () and system.getPreference ().

1 Like

This was exactly what I needed! Thank you!

I see that the samples you got are not simple at allā€¦

Look how I do what you want,
(some of my variables are declared elsewere, so use your own)

  1. lets declare above all a text box and create it in a ā€œcreateā€ phase:
 req_TB = native.newTextBox(display.contentCenterX, y, width_int, display.contentHeight/5*2)
 req_TB.isEditable = true -- default is false!
  1. add listener

req_TB:addEventListener( "userInput", Req_textListener_sub )

  1. put Req_textListener_sub above (in the lua-scope):

      local function Req_textListener_sub( event )
      	if event.phase == "editing" then
      		-- save text to settings every time the text is changed,
      -- alternatively you can save it once when submit button is clicked
      		our_text = req_TB.text
      		local tmpPref = {
      				req_txt = our_text
      				}
      			tmp_bool_a = system.setPreferences( "app", tmpPref)
      	end
      -- do not forget to clean up the saved text when you do not need it
      end
    
  2. in ā€œcreateā€ phase just read what is saved

     tmp_str_a = system.getPreference( "app", "req_txt", "string" )
     if tmp_str_a ~= nil then
     	if tmp_str_a ~= "" then
     		req_TB.text = tmp_str_a
     	end
     end