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 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
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
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
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)
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
Augusto Meneses
I have an android app and a stack to work onto. However, I can't save the stack. Can you point out what is wrong with this code?
on mouseUp
if the environment is "mobile" then
-- Check if write permission is granted
if mobilePermissionStatus("android.permission.WRITE_EXTERNAL_STORAGE") is "granted" then
put "Permission already granted" into field "debugOutput"
saveAndFeedback
else
-- Request write permission
mobileRequestPermission "android.permission.WRITE_EXTERNAL_STORAGE"
end if
else
-- Save the stack directly if not on mobile (e.g., desktop)
saveAndFeedback
end if
end mouseUp
-- Handle the permission response (automatically triggered by mobileRequestPermission)
on mobilePermissionResponse thePermission theStatus
if theStatus is "granted" then
put "Permission granted after request" into field "debugOutput"
saveAndFeedback
else
put "Permission denied" into field "debugOutput"
answer "Permission to write to storage was denied."
end if
end mobilePermissionResponse
-- Save the stack and provide feedback
command saveAndFeedback
-- Get the path to the documents folder (app's private storage)
put specialFolderPath("documents") & "/Masterpiece.livecode" into tSavePath
-- Debugging output to check the save path
put "Save path: " & tSavePath into field "debugOutput"
-- Save the stack to the specified path
save stack "Masterpiece" as tSavePath
if the result is empty then
put "Changes have been saved." into field "debugOutput"
answer "Changes have been saved."
else
put "Failed to save changes: " & the result into field "debugOutput"
answer "Failed to save changes:" & the result
end if
end saveAndFeedback
Panos Merakos
@Augusto
What happens when you click on the button? What messages do you get?