Saving App State

Moderators: LCNeil, heatherlaine, kevinmiller, elanorb

Post Reply
marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Saving App State

Post by marcelloe » Tue Dec 17, 2013 7:51 pm

I am trying to use the "on shutdown" and the following script to save the state of the app. When I use the home button the information is not save. Is the shutdown command not used when the home button is pushed? Should I use a different handler. What am I doing wrong?

Code: Select all

on shutdown
--set default folder to writeable directory on mobile
set the defaultFolder to specialFolderPath("Documents")
--puts the name of the current card into a text file called mobiletest.txt
put the short name of this card into URL ("file:mobiletest.txt")
pass shutdown
end shutdown

on openStack
--set default folder to folder with text file in it
set the defaultFolder to specialFolderPath("Documents")
-- places the contents of this text file into a temp variable
put URL ("file:mobiletest.txt") into tState
--navigates to the card names in the temp variable
go card tState
end openStack

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Saving App State

Post by LCNeil » Wed Dec 18, 2013 12:23 pm

Hi Mark,

This will only save and return to the current card you were last on. It will not save anything else (e.g. image locations, text in fields, slider positions etc)

If you wish to do this, then you will need to extract/save the contents/properties of each item of the card which state you want to save and then read this back in when the application opens.

I go into this process in some detail in the following idea2app forum post-

http://forums.runrev.com/viewtopic.php?f=73&t=17353

Kind Regards

Neil Roger
--
RunRev Support Team ~ http://www.runrev.com
LiveCode – Realize fast, compile-free coding


marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Saving App State

Post by marcelloe » Wed Dec 18, 2013 4:59 pm

This is the script I am using now, but I can't get it to work. What am I doing wrong?

Code: Select all

on openCard
   put specialFolderPath("Documents") & "/Data.txt" into tLocation
   put URL ("file:" & tLocation) into tData
   Put line 1 of tData into image "placeHolder1"
   Put line 1 of tData into image "placeHolder2"
   Put line 1 of tData into image "placeHolder3"
   Put line 1 of tData into image "placeHolder4"
   Put line 1 of tData into image "placeHolder5"
end openCard

on closeCard
   put specialFolderPath("Documents") & "/Data.txt" into tLocation
   put image "placeHolder1" && cr into tTemp
   put image "placeHolder2" && cr into tTemp
   put image "placeHolde3" && cr into tTemp
   put image "placeHolder4" && cr into tTemp
   put image "placeHolder5" && cr into tTemp
   put tTemp into URL ("file:" & tLocation)
end closeCard

elanorb
Livecode Staff Member
Livecode Staff Member
Posts: 455
Joined: Fri Feb 24, 2006 9:45 am

Re: Saving App State

Post by elanorb » Thu Dec 19, 2013 3:18 pm

Hi Mark

The only thing I can think of here is that the problem is caused by the fact that you are using lines to separate each images. Images are binary data, not text, and could therefore have new line characters within their data.

If you are saving image data the best option is to save each image as its own binary file, effectively creating image files on disk e.g.

on closeCard
put image "placeHolder1" into url ("binfile:" & specialFolderPath("Documents") & "/placeHolder1.jpg")
put image "placeHolder2" into url ("binfile:" & specialFolderPath("Documents") & "/placeHolder2.jpg")
put image "placeHolder3" into url ("binfile:" & specialFolderPath("Documents") & "/placeHolder3.jpg")
put image "placeHolder4" into url ("binfile:" & specialFolderPath("Documents") & "/placeHolder4.jpg")
put image "placeHolder5" into url ("binfile:" & specialFolderPath("Documents") & "/placeHolder5.jpg")
end closeCard

If necessary you could store the file paths in your Data.txt file.

I hope that helps.

Kind regards

Elanor
Elanor Buchanan
Software Developer
LiveCode

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Saving App State

Post by marcelloe » Thu Jan 16, 2014 4:21 pm

This is what I have got and it stores the the image but doesn't put it back into the correct place. What am I doing wrong?

Code: Select all

on closeCard
   put image "placeHolder1" into url ("binfile:" & specialFolderPath("Documents") & "/placeHolder1.jpg")
   put image "placeHolder2" into url ("binfile:" & specialFolderPath("Documents") & "/placeHolder2.jpg")
   put image "placeHolder3" into url ("binfile:" & specialFolderPath("Documents") & "/placeHolder3.jpg")
   put image "placeHolder4" into url ("binfile:" & specialFolderPath("Documents") & "/placeHolder4.jpg")
   put image "placeHolder5" into url ("binfile:" & specialFolderPath("Documents") & "/placeHolder5.jpg")
end closeCard

on openCard
   put url ("binfile:" & specialFolderPath("Documents") & "/placeHolder1.jpg") into image "placeholder1"
   put url ("binfile:" & specialFolderPath("Documents") & "/placeHolder2.jpg") into image "placeholder2"
   put url ("binfile:" & specialFolderPath("Documents") & "/placeHolder3.jpg") into image "placeholder3"
   put url ("binfile:" & specialFolderPath("Documents") & "/placeHolder4.jpg") into image "placeholder4"
   put url ("binfile:" & specialFolderPath("Documents") & "/placeHolder5.jpg") into image "placeholder5"

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Saving App State

Post by LCNeil » Fri Jan 17, 2014 2:23 pm

Hi Mark,

In order to set the image from the created image binary, you would need to use something like this-

Code: Select all

set the filename image "placeholder1" to (specialFolderPath("Documents") & "/placeHolder1.jpg") 
Kind Regards,

Neil Roger
--
RunRev Support Team ~ http://www.runrev.com

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Saving App State

Post by marcelloe » Fri Jan 17, 2014 4:28 pm

The imaged is saved, but once the app restarted the image takes up the whole screen. What am I doing wrong?

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Saving App State

Post by LCNeil » Fri Jan 17, 2014 4:39 pm

Hi Mark,

This will happen if you do not have not locked the size and position of your image areas.

You can turn this option on by selecting your image areas within the LiveCode IDE, opening the property inspector, navigating to "size & position" and then checking the box "Lock Size and Position"

Kind Regards,

Neil Roger
--
RunRev Support Team ~ http://www.runrev.com

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Saving App State

Post by marcelloe » Wed Jan 22, 2014 8:59 pm

How would I go about saving the data of Pictures and text fields on multiply cards? I want this to be saved after the app is closed.

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Saving App State

Post by marcelloe » Fri Feb 07, 2014 7:05 pm

How would I save the state of a button. I have a button that will change the color of the button. I want to be able to save wich color the button is at the time the app is closed?

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Saving App State

Post by LCNeil » Mon Feb 10, 2014 3:04 pm

Hi Mark,

Depending on how you are changing the color of the button (icon / backgroundColor) then you should be able to write our that property to your state text file-

e.g. to get the backGroundColor (fill) of a button is-

put the backGroundColor of button "<name of button here> into <destination here>

then you would read it from the destination to set it again.

Kind Regards,


Neil Roger
--
RunRev Support Team ~ http://www.runrev.com
--

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Saving App State

Post by marcelloe » Mon Feb 10, 2014 6:36 pm

This is the script I am using, but I am missing something.

Code: Select all

on closeCard
 put the backgroundColor of button "button 1' into url ("file:" & specialFolderPath("documents") & "/button 1.txt")
end closeCard

on openCard
put  url ("file:" & specialFolderPath("documents") & "/button 1.txt") into button "button 1"
end openCard

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Saving App State

Post by LCNeil » Tue Feb 11, 2014 11:24 am

Hi Mark,

The script you have was almost correct. Please make sure that you are closing your strings with speech marks as this could cause errors from the start.

Code: Select all

on closeCard
   put the backgroundColor of button "button 1" into url ("file:" & specialFolderPath("documents") & "/button 1.txt")
end closeCard

on openCard
   set the backgroundColor of  button "button 1" to url ("file:" & specialFolderPath("documents") & "/button 1.txt")
end openCard
Kind Regards,


Neil Roger
--
RunRev Support Team ~ http://www.runrev.com
--

Post Reply

Return to “idea2app and Coding School”