Problem accessing variables

Smee

Well-Known Member
Licensed User
Longtime User
I have a variable in the main activity called PageNumber. In a secondary activity i save the current page thus

B4X:
Sub Activity_Pause (UserClosed As Boolean)
   Main.PageNumber= PageTurner.CurrentPage
   Main.CatNum=CatNum
   PageTurner.OnPause
End Sub

Sub Activity_Resume
   Log (Main.PageNumber)
   Log(Main.CatNum)
   PageNumber=Main.PageNumber
   CatNum=Main.CatNum
   PageTurner.CurrentPage = PageNumber
   PageTurner.OnResume
End Sub

when resuming the activity from the main module usng StartActivity the variables are Zero. They are Dim in Process_Globals.

Where am i going wrong :confused:
 

Smee

Well-Known Member
Licensed User
Longtime User
Thanks for the reply Erel

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim SQL As SQL
   Dim PageNumber,CatNum As Int
   Dim ProgressBar1 As ProgressBar
   Dim TotalDownloaded,TotalSize As Long
End Sub

Nope definitely in the main module and definitely in the above. I copied and pasted.
i also prefix the storing of the variable with Main. and use the auto-finish so as to avoid typos etc
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Are you setting the variables to zero in the Sub Activity_Create or in Sub Activity_Resume?

If you set them to zero in Activity_Create then make sure you only do so when FirstTime is true. If you are doing it in Activity_Resume then don't!
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
No. I initially set them to zero and i thought that was what caused the problem so i removed those lines. The activity resume that i posted is the complete code. I put a log in to see the figures and they were zero when the activity resumed. I tried saving the variables to another module but got the same result. I also logged the page number before pausing the activity and it showed that the main.pagenumber was being set at 3
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Just so we are clear on the events:

1) In the main module you are setting PageNumber and CatNum to values.
2) When you call the secondary activity the values are zero

If the above is correct I would check what the values are just before the secondary activity is called (which you appear to have done), and also examine the main activities pause event to see if that is resetting the values.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Just so we are clear on the events:

1) In the main module you are setting PageNumber and CatNum to values.
2) When you call the secondary activity the values are zero

If the above is correct I would check what the values are just before the secondary activity is called (which you appear to have done), and also examine the main activities pause event to see if that is resetting the values.

No the values are only set in the main activity by the secondary activity when the secondary activity has been paused. when the secondary activity is resumed the values are called from the main activity. Does this make sense?
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
i am starting to think it may be a problem with the device.

i simplified the whole thing by pasting this into the second activity pause event

Main.PageNumber= 3

and in the Main
B4X:
Sub Activity_Resume
Log(PageNumber)
End Sub

and i get a zero

** Activity (catalogues) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
0

But why am i getting is first = true when i am returnin g to the first activity?


Erel, sorry it is not practical to post cause the program uses a large database
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
How are you calling the second activity, and do you close the main activity after you have called the second one?
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
How are you calling the second activity, and do you close the main activity after you have called the second one?

StartActivity(Catalogues)

i thought that the main activity just pauses when calling another activity/

I exit the second activity by pressing the home key

I thought this was supposed to preserve the variables

I am pressing the home key and it is definitely restarting the first activity. I put a log message in the following

B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime=True Then
      Log ("Yes")
....

end sub

and it prints yes each time.

strange????
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I posted my previous post while you were posting yours.

If your device is low on memory it might kill the whole process while no activity is displayed.
For settings that should preserve in such case, you should create a settings file.
You can use StateManager to help you: Basic4android Search: StateManager
You can also create a Map with the values and store the Map with File.WriteMap.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Try this code, paste this into main in a new project
B4X:
'Activity module
Sub Process_Globals
   Dim PageNumber,CatNum As Int
End Sub

Sub Globals
Dim btnTest As Button 
End Sub

Sub Activity_Create(FirstTime As Boolean)
btnTest.Initialize ("new")
If FirstTime Then
   PageNumber=0
   CatNum=0
   btnTest.Text = "Second Activity"
   Activity.AddView (btnTest,0,0,200,200)
End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub new_Click
   Log ("PageNumber = "&PageNumber)
   Log ("CatNum = "&CatNum)
   StartActivity (Increment)
End Sub

then create a new activity module named Increment and paste this code
B4X:
Sub Process_Globals
End Sub

Sub Globals
   Dim PageNumber,CatNum As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim btnExit As Button 
   btnExit.Initialize ("exit")
   btnExit.Text = "Exit and increment"
   Activity.AddView (btnExit,0,0,200,200)
End Sub

Sub Activity_Resume
   PageNumber = main.PageNumber 
   CatNum = main.CatNum 
   Log ("Main.PageNumber = "&Main.PageNumber)
   Log ("Main.CatNum = "& Main.CatNum)
   CatNum= CatNum+1
   PageNumber=PageNumber+1
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   Main.PageNumber = PageNumber
   Main.CatNum = CatNum
      Log ("Main.PageNumber = "&Main.PageNumber)
   Log ("Main.CatNum = "& Main.CatNum)
End Sub
Sub exit_Click
   Activity.Finish
End Sub

When you click the buttons, you should see PageNumber and CatNum being incremented in the logs.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
thanks for the replies guys. I will try tonight when i get home from work
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
When you click the buttons, you should see PageNumber and CatNum being incremented in the logs.

Well that works ok. So i guess it must be cos i am pressing the home button. I will put a menu item in to exit the second activity and see if that fixes the problem
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Well that works ok. So i guess it must be cos i am pressing the home button. I will put a menu item in to exit the second activity and see if that fixes the problem

Pressing the Back button whilst in the Increment activity should still increment the variables and go back to the main activity.

Note: I regard the "Home" button to be the one that takes you back to the home screen, wherever you are in the app, and the "Back" button to be the one that takes you out of the current activity.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Ok something extremely strange happening

Below is the copy of the logs

1/70 /cats/baby/BABY 124.jpg
2/70 /cats/baby/BABY 140.jpg
3/70 /cats/baby/BABY 168.jpg
** Activity (catalogues) Pause, UserClosed = true **
Main.PageNumber = 3
Main.CatNum = 0

up to here all is good. 3 pages turned, exit activity and the pagenumber is stored in Main.

Now after exiting the activity the Main is running thru the first time create again, thus re-setting everything

** Activity (main) Create, isFirst = true **
Yes
** Activity (main) Resume **
0

And here is the relevant first time

Sub Activity_Create(FirstTime As Boolean)
If FirstTime=True Then
Log ("Yes")
etc

end sub


So why would the main activity re-start each time? I used your code to exit the second activity

Sub mnuExit_Click
Activity.Finish
End Sub

:BangHead::BangHead::confused:
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
As I asked in post #10
How are you calling the second activity, and do you close the main activity after you have called the second one?

Post up your sub that calls the second activity.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
B4X:
Sub btnAction_Click
   Dim btn As Button 
   btn = Sender 
   BtnVal=btn.Tag   
   Select BtnVal
   Case 0
      Log (PageNumber)
      StartActivity(Catalogues)
   End Select
End Sub

Second Activity

Sub mnuExit_Click
   Activity.Finish
End Sub
 
Upvote 0
Top