Android Question How to Keep public variable when device get into deep sleep

karyadi

Member
Licensed User
Longtime User
Hi All,

i have a question,
How to Keep public variable when device get into deep sleep?
because every time my device get into deep sleep, all my variable become null.

thanks
 

karyadi

Member
Licensed User
Longtime User
How and where do you declare them ?

Hi Klaus, i declare on my Main activity

Sub Process_Globals.



i already try using starter service to keep my variable, but it's doesn't work.


Save them to a file (Activity_Pause) or at your need. Reload it (Activity_Resume) or at your need.

Yes your advice can be use, or maybe there is more simple way to keep thje variable?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
simple way to keep thje variable?
Yes. The starter service.

If it does not work for you then you should upload your project showing the problem.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
That values of global variables are lost and the app resume without them.
That would mean I did huge mistakes in programming and I should put hands on all my apps if really can happen.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
If the app is killed, then when restarted you have all the reinitialuzation needed.
What I can't believe is the app is just resumed with lost variables.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Maybe I didn't get the question but I want to be sure.
Look to this simple project

B4X:
Sub Process_Globals
    Dim test1 As String
End Sub

Sub Globals
    Dim test2 As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    test1="hello"
    test2="hello"
End Sub

Sub Activity_Resume
    Log(test1)
    Log(test2)
End Sub

Can I be sure that each time the app is started or resumed the variables test1 and test2 contain "hello" ?
Does it exists any situation the app is resumed without those variables ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are several mistakes in this code:
1. Don't use Dim in Process_Globals. Use Private or Public instead.
As a general rule public process global variables should be declared and set in the Starter service (Service_Create).

2. Non-UI objects should be declared in Process_Globals instead of Globals.
3. Process_Global variables should only be set when FirstTime is true (in this case it doesn't really matter).
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Ok.
Talking about apps written and published before the introduction of the starter_service in B4A, I put again the question about this code:

B4X:
Sub Process_Globals
    Public test1 As String
End Sub

Sub Globals
    Public test2 As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime=true then
       test1="hello"
       test2="hello"
   End If
End Sub

Sub Activity_Resume
    Log(test1)
    Log(test2)
End Sub

Can I be sure that each time the app is started or resumed the variables test1 and test2 contain "hello" ?
Does it exists any situation the app is resumed without those variables ?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
So:

B4X:
Sub Process_Globals
    Public test1 As String
End Sub

Sub Globals
    Public test2 As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime=true then
       test1="hello"
   End If
   test2="hello"
End Sub

Sub Activity_Resume
    Log(test1)
    Log(test2)
End Sub

If this code is correct, is it the answer to karyadi (first post) ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'm afraid no.
1. Global variables cannot be public (though this is not a real issue).
2. Process global variables should be declared and set in the starter service. This is a real issue.

Assuming that Main.test1 is accessed from Activity2 then at some point Activity2 will start before Main and the variable will not be set.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Need to print this topic, append to the wall and read it every day. Much confusion in my head.
But again I want to put the question in a simpler way (same code as above):
Suppose the app start normally from Activity1 and test1 and test2 are both set to "hello".
These two variables, are kept forever (until the app is closed or killed by OS of course) or can happen the app RESUME with NULL in them ?
 
Upvote 0
Top