Android Tutorial StateManager - Helps managing Android applications settings and state

Status
Not open for further replies.

Felix Maria

Member
Licensed User
Longtime User
Yes. I have a Main Activity. Under the Activity, I have 4 layout (.bal) which i load during the runtime with activity.loadlayout
When I navigate from say first.bal to sec.bal and then come back to first.bal....The status of the first.bal is initialised.
how do we preserve the state of the layout first.bal like retaining the textbox entries and the spinner list etc....
Thank U!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
A layout file is nothing more than a table of views that will be added to a panel or activity.

StateManager can only save and restore the state if the views were added in the exact same order. So if your program loads everything in Activity_Create and then only changes the visible panel then it will work. If you are loading and removing views at runtime it will not work.
 

Felix Maria

Member
Licensed User
Longtime User
Thank U! Have never experienced a Support quicker than this.
I have changed all the layouts to panels. Now it seems to be fine with the visible property.
 

Mark Zraik

Member
Licensed User
Longtime User
StateManager is a code module which takes care of handling the application UI state and settings.
Just wanted to say thank you for the hard work on this. Playing with it now.


Respectfully,
Coroner
 

Mark Zraik

Member
Licensed User
Longtime User
Hello All,

I have been fartin' around with creating/re-creating a bunch of apps in B4A. I started using the StateManager and it really does the job if you think it through.
The 1 thing I couldn't determine for sure is whether I wanted files kept on the DirInternal or DirExternal or even DirRootExternal path.
So, I abused the StateManager code module, added a sub to handle this, and thought I would throw it up here for all to continue to abuse.

The way it is setup is: It defaults to the DirExternal, but if that's not available, then DirRootExternal(Psuedo External, I think), and then DirInternal.
By simply re-ordering the if statements, you can easily change the order of preference.
You could also change the DirXXX to anything else you wanted.
For my apps and the way I code them, I prefer to use the DirExternal for state and settings, but if that's not available, then it falls to the other possible
and existing directory choices.
I have DirRootExternal commented out, but it does work, I just didn't need it for my coding.

If there is a better way to do what I have done, by all means share!
NOTE:I changed the name of the StateManager code module to IXStorage, so it could be used side by side.
Then, I did the following as posted in the UPDATE below.

UPDATE: 09/07/2014
After considerable research, reading post from here and all over the net, it appears that
the only reason to use Dir...External would be to allow access to your file/s from outside your program by putting them in the public or sharable directory of your app (determined by the OS, ofcourse), which on occasion you may want to do. DirDefaultExternal may/usually/actually points to a partition on the Internal Storage card that the OS considers Public and sharable. Android does post on the developer site, that starting with level 18 / Android 4.4, that it will no longer be necessary to add a manifest permission to write to the External Storage (for backward compatibility one should add this version of the permission: AddPermission(android.permission.WRITE_EXTERNAL_STORAGE android:maxSdkVersion="18"), but they didn't state (that I could find), whether or not a developer had physical access to the physical storage sdcard. So, even choosing File.DirRootExternal may not get you to the physical sdcard you spent extra money on due to symbolic links the OS may be using. I made a few adjustments to my original code mods to Erels StateManager, renamed this new version ShareSettings.

From http://developer.android.com/guide/topics/data/data-storage.html
Annotated-

In this version I changed the If File.IsDirectory test to If File.ExternalWritable = True, which is a solid test to see if you can write to External Storage in the first place. If it fails, then it defaults to DirInternal.

Next, I'm going to look into splitting the code in such a way to have a private state.dat and a shared settings.properties file. I think it could come in handy and save a few keystrokes if you needed it.

ANOTHER NOTE: The research from the developer site also pointed out that DirDefaultExternal along with DirInternal will be deleted when the app is uninstalled. This has impact if the user has content that belongs to the user and not the app and should remain after uninstall. It appears that DirRootExternal might be the place to put these types of files so they remain after uninstall... I'm looking into that, and maybe someone else already knows and could shed some light on this for the rest of us.

UPDATE2: I did the split -attached as ShareSettingsOnly! Private DirInternal for state.dat, DirDefaultExternal for settings.properties. It will still fall back to internal, if External is not writable!
 

Attachments

  • IXStorage.zip
    9.1 KB · Views: 425
  • ShareSettings.zip
    2.8 KB · Views: 405
  • ShareSettingsOnly.zip
    2.8 KB · Views: 374
Last edited:

kabron

Member
Licensed User
Longtime User
StateManager does not work with runtime created objects

Everything works fine in the StateManager example if the Activity is filled in the Activity_Create Sub.
But if it is altered at runtime, the info about new object is lost somewhere.
I created a Sub to add a button on ToggleButton1 click and it works fine until screen rotate. I also Log (Activity.NumberOfViews)
and it is seen that NumberOfViews was increased after AddButton event and in SaveState Sub, but it decreased in RestoreState.
I modified innerSaveState and innerRestoreState to work with Buttons. It does not help.
Also I noticed that code for Buttons is executed in innerSaveState and innerRestoreState, even if there is no any Buttons.
The code attached.
I have no idea, any help appreciated
 

Attachments

  • StateMan.zip
    11.2 KB · Views: 386

kabron

Member
Licensed User
Longtime User
Thanks for answer, Erel
If I understand correctly, StateManager is not usefull with objects, created during runtime. Are there any other solutions?
 

kabron

Member
Licensed User
Longtime User
Unfortunately my objects are creating and destroying randomly at runtime.
But, how it works, say in games? It is quite the same case.
 

kabron

Member
Licensed User
Longtime User
My requirements are very simple: a few buttons, labels with text created and modifying during runtime and some drawings on canvas.
 

Mark Zraik

Member
Licensed User
Longtime User
My requirements are very simple: a few buttons, labels with text created and modifying during runtime and some drawings on canvas.
Hi kabron,
I had some thoughts, but after a time(and not being a game coder, I decided not to join in just yet.
I don't mean to take up your time and anyone elses, but I couldn't find the cancel button.

Good luck in your game coding!
 

vinnythepooh71

Member
Licensed User
Longtime User
I am using the statemanager and i get these warnings :

return type ( in sub signature ) should be set exsplicitly

'Gets the setting value associated with the given key.
'Returns an empty string if the key was not found.
Sub GetSetting(Key As String)
Return GetSetting2(Key, "") <--------------------------this line
End Sub

return type ( in sub signature ) should be set exsplicitly

'Gets the setting value associated with the given key.
'Returns the DefaultValue parameter if the key was not found.
Sub GetSetting2(Key As String, DefaultValue As String)
If settings.IsInitialized = False Then
'load the stored settings
If File.Exists(File.DirInternal, settingsFileName) Then
settings = File.ReadMap(File.DirInternal, settingsFileName)
Else
Return DefaultValue <-----------------------this line
End If
End If
Dim v As String
v = settings.GetDefault(Key.ToLowerCase, DefaultValue)
Return v <--------------------------- and this line
End Sub

return value si missing ( using default )

'Loads the stored state (if such is available)
'ActivityName - Should match the value use in SaveState
'ValidPeriodInMinutes - The validity period of this state measured in minutes. Pass 0 for an unlimited period.
'Returns true if the state was loaded
Sub RestoreState(Activity As Activity, ActivityName As String, ValidPeriodInMinutes As Int) As Boolean
Try
loadStateFile
If states.IsInitialized = False Then
Return False
End If
Dim list1 As List
list1 = states.Get(ActivityName.ToLowerCase)
If list1.IsInitialized = False Then Return <------------------------------ this line
Dim time As Long
time = list1.Get(0)
If ValidPeriodInMinutes > 0 AND time + ValidPeriodInMinutes * DateTime.TicksPerMinute < DateTime.Now Then
Return False
End If
listPosition = 0
For i = 0 To Activity.NumberOfViews - 1
innerRestoreState(Activity.GetView(i), list1)
Next
Return True
Catch
Log("Error loading state.")
Log(LastException.Message)
Return False
End Try
End Sub


Sorry for the verbosity, but should i worry ???
 

Fusseldieb

Active Member
Licensed User
Longtime User
Which command I need to use to save the entire screen (all visible items, such as listviews, scrollview, labels, etc...)?
 

chrjak

Active Member
Licensed User
Longtime User
Hey Fusseldieb

2 possibilitys:
B4X:
StateManager.SaveState(Activity, "Main")
StateManager.SaveSettings
This saves whole Activity

but if that doesn't work you can try it in a loop! e.g:
B4X:
For i = 0 to Activity.NumberOfViews - 1
'Test and backup here every view on activity
next
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…