Android Question Storing Activity State in External Class

DawningTruth

Active Member
Licensed User
I need to create an external class for storing activity data during the activity lifecycle.

Here is the envisioned Sequence:






The Activity is quite complex. Having sophisticated custom elements.

So I need to be able to carry the state across from activity destruction to activity recreation. I understand that UI elements cannot be serialised so it is not possible to save the activities in a datastore. So, I will have to save them in a persistent class.

Now to the question. How is this done?

I have tried declaring the class in my Main Activity, and then using it in the above activity (Working Activity), but it keeps giving me an error. If I declare the class in the Working Activity, then it is destroyed when the activity is destroyed during the pause event.

By declared I am using code like:

B4X:
'In Working Activity
Private StorageClass1 As StorageClass

'When I tried Main Activity Declaration
Public StorageClass1 As StorageClass

Any suggestions?
 
Last edited:

MarkusR

Well-Known Member
Licensed User
Longtime User
there is something ready that save/load the ui state
i believe it was this StateManager
https://www.b4x.com/android/forum/t...applications-settings-and-state.9777/#content

its also possible that android not destroy the activity.

if you put the declaration in Process_Globals
Public StorageClass1 As StorageClass
and it was initialized
StorageClass1.Initialize once
you can access it via Main.StorageClass1. ...
in StorageClass use a public sub or properties set/get
 
Last edited:
Upvote 0

DawningTruth

Active Member
Licensed User

Thx Markus.

I managed to get the second option working
 
Upvote 0

DawningTruth

Active Member
Licensed User
Ok, have tried the suggested approach. It is retaining the state of the elements, however when I try to recreate the view in Landscape, none of the elements are transferring across.

So for instance I have a label value. I copy the lbl_MyLabel UI element to Main.StorageClass1.MyLabel at pause. However when I copy the value back into lbl_MyLabel during the resume process. Nothing is copied back into the label value.

Any suggestions?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is retaining the state of the elements, however when I try to recreate the view in Landscape, none of the elements are transferring across.
I'm not sure what you mean here however you cannot reuse the same views after the activity was destroyed.

Check the suggested state manager module. It saves the user related state and sets it again when the activity is created.
 
Upvote 0

DawningTruth

Active Member
Licensed User
I'm not sure what you mean here however you cannot reuse the same views after the activity was destroyed.

Check the suggested state manager module. It saves the user related state and sets it again when the activity is created.
Thx Erel, I will look at it. Does it support all the B4A views?
 
Upvote 0

DawningTruth

Active Member
Licensed User
The question is not accurate enough for me to give a full answer. It only saves user related state. It supports all native views. In some cases you will need to do some work to get the state of all views (see the code, it is quite simple).
Thx Erel, I will investigate further...
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
It is retaining the state of the elements, however when I try to recreate the view in Landscape, none of the elements are transferring across.
if android not recreate the activity / ui then u need handle the new orientation by yourself.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…