Save the layout when the app is closed

philippek

Member
Licensed User
Longtime User
Hi Guys,

Im kinda new here, but I have a question. I've made a app with diferent layouts, and I want the app to save the layout where the user is in when they press the 'save' button. And when they close the app and start the app again, that the app still know wich layout the user in is. But I don't know how to do this. I thought about a SQL database, but it isn't really data for in a table, so do you guys have some advice?

I haven't worked a lot with Basic4android, but i've done alot with VB (also with databases).

So if anyone can help that would be great!
Thanks in advance.

Philippek
 
Last edited:

klaus

Expert
Licensed User
Longtime User
One solution could be, having one or several process global variables to memorize what layout is active, save these variables in a file in Sub Activit_Pause. When the user runs the program, load the setup file and display the right layout.

Best regards
 
Upvote 0

philippek

Member
Licensed User
Longtime User
Thanks for your reactions, but it's still not working

@Klause I don't know how to do this.

@Erel EDIT: I'm now trying a other way.
B4X:
Sub btnHerbergSave_Click
   numbersave = 1
   StateManager.SetSetting("savegame", True)
   StateManager.SaveSettings
End Sub
B4X:
Sub Process_Globals
Dim numbersave As Int
      End Sub
B4X:
Sub savegamenumber
If numbersave = 1 Then
 exitmenu
 activity.LoadLayout("eersteherberg")
 Else
 Msgbox("No Save Game Found", "error")
 End If
 End Sub
B4X:
Sub btnMenuCon_Click
      StateManager.GetSetting("savegame")
      savegamenumber
End Sub
But how can I make that the 'savenumber' is saved when the program is closed and opend again?
 
Last edited:
Upvote 0

philippek

Member
Licensed User
Longtime User
Thanks Erel it's working now :). I will put you in the credits (also for making Statemanager).
:sign0098:.
Philippek

EDIT: Well now I have the following problem, the program saves the number good if the program is closed with the exit button (activity.finish). But if the program is closed with the home button and then killed with a taskmanager then is the savegame number reseted.
 
Last edited:
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Are you only saving the state if UserClosed = True (in the Activity_Pause sub)?

If I'm not mistaken, the Activity_Pause sub gets fired any time the activity "loses focus" so to speak, such as if the phone goes to sleep, the user presses Home, etc. In this case, I would think the app should save the settings when the user presses Home so long as you are NOT only saving the state when UserClosed = True.


Even if it doesn't work this way, this should also be a good demonstration of why apps shouldn't be closed using a task manager. Although I realize you can't control what other users do.

Edit to add: Some things about the state do need to be saved when the app is paused in order to restore them later. For example, after changing the orientation of your device (because doing this will wipe out things such as text in an EditText). However, I don't believe that you HAVE to save your app's settings only in Activity_Pause. It looks like originally you were saving the settings in a different sub. So long as nothing can change with the settings between that sub firing and the app closing, I see no reason why you cannot change the app back to saving the settings as you did before (but still restore them in Activity_Create). My app loads them in Activity_Create (when FirstTime = True) and saves them in a sub other than Activity_Pause. There are certain user settings in my app that can change when the user does something specific and when that happens, I save the settings at that time. Perhaps I am saving settings "too often", but my app never loses settings. Since my app only supports one orientation, I don't need to worry about losing contents of an EditText.
 
Last edited:
Upvote 0

philippek

Member
Licensed User
Longtime User
Hi Kevin thanks for your reply :)
Well I've tried alot with userclosed = true and = false but it doesn't seem to work.
Now I have this code, it's saving the settings double.
B4X:
Sub Activity_Pause (UserClosed As Boolean)
timerbegin.Enabled = False
StateManager.SaveSettings
If userclosed = True Then
StateManager.SaveSettings
Else 
statemanager.SaveSettings
End If
End Sub
Any ideas, or is it just impossible to save if the app is killed with a taskmanager.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Hi Kevin thanks for your reply :)
Well I've tried alot with userclosed = true and = false but it doesn't seem to work.
Now I have this code, it's saving the settings double.
B4X:
Sub Activity_Pause (UserClosed As Boolean)
timerbegin.Enabled = False
StateManager.SaveSettings
If userclosed = True Then
StateManager.SaveSettings
Else 
statemanager.SaveSettings
End If
End Sub
Any ideas, or is it just impossible to save if the app is killed with a taskmanager.

It is saving the settings twice because of the first StateManager.SaveSettings and because that same statement fires again in your If/Then statement no matter what value UserClosed contains.

I would think that it would work without the If/Then statement altogether such as:

B4X:
Sub Activity_Pause (UserClosed As Boolean)
timerbegin.Enabled = False
StateManager.SaveSettings
End Sub

I don't know how your app works or what it does, but is it possible to save the settings by calling a sub if the user changes something that needs to be saved? For example, my app allows the user to select from 3 different layouts to use. When the user chooses a layout from the menu, I store the chosen layout in a variable and call a sub to save all of my app settings (variables).

I should point out that I do not use the state manager, so perhaps there is some other trick or caveat involved.
 
Upvote 0

philippek

Member
Licensed User
Longtime User
The code that you gave was the original code that I used, but that one also didn't work.
Actually my app also kinda works like your app. My app uses a variable to say wich layout the user is in.
For example:
If the user is in layout 1 the savenumber is 1
when the user is in layout 2 the savenumber is 2. When the user closes the app and starts it again, the app checks wich savenumber it has and runs the layout.
But would you please show me the code of the sub wich saves all your setting in your app, I think that could help me out!
Thanks in advance!
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I am not familiar with the state manager (or your code) but this is a simplified version of what I do:


B4X:
Sub Activity_Resume
  StateManager.GetSetting("savegame")
  UseLayout = savegame ' Set a global variable that keeps track of current layout
  Select Case UseLayout
    Case 1
      Activity.LoadLayout("MyAppLayout1")
    Case 2
      Activity.LoadLayout("MyAppLayout2")
    Case 3
      Activity.LoadLayout("MyAppLayout3")
    Case Else
      Msgbox("No Save Game Found", "error")
  End Select
End Sub
 
 
' THE FOLLOWING SUB NAMES SHOULD BE WHATEVER SUBS OR BUTTON
' EVENTS GET FIRED WHEN THE USER WANTS TO CHANGE TO A DIFFERENT
' LAYOUT
 
 
Sub Layout1_Click
  ' CODE TO SET THE LAYOUT OR CHANGE THE APPEARANCE
  ' .........................
 
  ' Save last setting for the user
  UseLayout = 1
  StateManager.SetSetting ("savegame", UseLayout)
  StateManager.SaveSettings
End Sub
 
Sub Layout2_Click
  ' CODE TO SET THE LAYOUT OR CHANGE THE APPEARANCE
  ' .........................
 
  ' Save last setting for the user
  UseLayout = 2
  StateManager.SetSetting ("savegame", UseLayout)
  StateManager.SaveSettings
End Sub
 
Sub Layout3_Click
  ' CODE TO SET THE LAYOUT OR CHANGE THE APPEARANCE
  ' .........................
 
  ' Save last setting for the user
  UseLayout = 3
  StateManager.SetSetting ("savegame", UseLayout)
  StateManager.SaveSettings
End Sub


Again, I really don't know what your app is doing or how, but hopefully this will push you in the right direction. :)
 
Upvote 0
Top