Save Current State On Mobile Device

By default, when you exit  and then re-enter a LiveCode created mobile app no state is saved and your application does not resume from where it left off.

This lesson will explain how to create a simple app, save the state of an app to a text file when the app closes and when re-opened, have this state re-applied.

Create a Stack with Two Cards

Create a Stack with Two Cards

Create a new stack with two cards. Card one named "one" and Card two names "two". I have set the size of this stack to 320 x 480

Create some navigation buttons so you can go to and from each card (go next card, go previous card)

Now, create some graphics on each card so that when you navigate, you can tell you are on a new card

Create a file that saves the state on shutdown

Create a file that saves the state on shutdown

We will want to create/add to this file when a shutDown request is triggered. This is possible is we use the shutDown message

The following is added to the stack script:

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

 

Add a way to send the shutdown signal

In order to handle shutDown, shutDown must first be sent. Since this may not occur by way of dismissing it in the recent apps screen, the app itself must provide its own means.

An additional button is added and the following is added to its script:

on mouseup
   quit 0
end mouseup

Load this state from the text file

Load this state from the text file

Now that we have saved the state, we will want to load it when the stack is opened. We can do this with the openStack message.

In the stack script, add the following:

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

Deploy to device/sim

Deploy to device/sim

Now when you deploy to a device/sim, exit the app and then re-open the last card that you navigated to will be the one show.

Although this is a very basic example, the same principles can be applied to content within an application as well (e.g. text fields, button states, control positions etc)

 

 

4 Comments

Tom B.

Thanks for this tutorial. How would you save an array containing a wide range of state information from the app? Can an array be saved as a file or is it better to store it in the cprop of a stack?

Elanor Buchanan

Hi Tom

You can use the arrayEncode function to serialise an array as binary data which you can then save out to file. When you want to read it in again use arrayDecode to convert the binary data back to an array.

There is also a lesson on reading and writing to files at

http://lessons.runrev.com/m/4603/l/44093-reading-and-writing-to-file

Kind regards

Elanor

Ron

Hello livecode support team,

I am a begginer user of livecode and while making my app i faced a problem where the user progress doesn't save after he closes the App.

While looking for a way to resolve, I found this helpful tutorial at http://lessons.livecode.com/m/4069/l/152290-save-current-state-on-mobile-device but when i copied and applied the code )on the stack script) it had 2 errors.

1.compilation error at line 21 (Chunk: can't create a variable with that name (explicitVariables?)) near "tState", char 35 which got resolved by switching the "into" to "to".

2.compilation error at line 21 (Expression: unquoted literal), char 35 this error I made disappeared by going to edit and then unmarking the variable check option.

However, although the code was implemented according yo your guide & there are no compilation errors right now, after testing the app on the phone it still didn't work. I was wondering if there's any way to fix this problem and enable my app users to save their progress after they shut down the app.

Please see below the relevant section on the project code,

I appreciate your prompt response,

Ron

Code:
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") to tState

--navigates to the card names in the temp variable

go card tState

end openStack

Elanor Buchanan

Hi Ron

The errors happen of you have variable checking on, which requires variables to be declared before use. You can turn this off in the Edit menu. Alternatively you can declare the variables before they are used.

If you turn off variable checking and then change the "to" back to "into" in the line

put URL ("file:mobiletest.txt") to tState

this code should work.

I hope that helps.

Kind regards

Elanor

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.